I want to write a for loop that iterates over a list. The list is a deck of "cards". There are 16 cards, numbered 0 - 7. Each number appears in the list twice.
When I comment the for loop out, the program correctly displays 1 card on the left side of the canvas. But when I use the loop, nothing's displayed. The code is incomplete, it needs to be run using a particular online software. The link to the entire program is below.
I want to use the for loop to populate the canvas with the 14 numbers in the range 0-7.
Here's the code, let me know if you're able to spot the error.
C_W = 50
C_H = 98
#upper left corner of a card:
u_l = [1 , 1]
deck = range(0,8)
deck1 = deck
#called by a button on the GUI
# cards are 50x100 pixels in size
global C_W, C_H, deck
count = 0
for card in deck:
canvas.draw_text(str(deck[count]), (C_W / 3, C_H - 25), 60, "White")
C_W += C_W
count += 1
draw() function in simplegui is called about 60 per second and the code in the
for loop inside your version doubles the global
C_W 16 times each time the function is called. This means its value quickly becomes astronomical and the text is drawn way off the screen to the right somewhere outside our galaxy.
Here's a simple way to avoid that problem:
def draw(canvas): global C_W, C_H, deck count = 0 c_w = C_W # init with value of global variable for card in deck: # note "card" is not being used canvas.draw_text(str(deck[count]), (c_w / 3, C_H - 25), 60, "White") c_w += c_w count += 1