Ekaterina1234 Ekaterina1234 - 4 months ago 10
Python Question

For loop supposed to print 4 rows. it only prints one

I have a list called deck, which has 104 elements. I want to create a for loop, that displays images on a canvas in a simple GUI (that can only be run on CodeSkulptor, the link to my program's here:
http://www.codeskulptor.org/#user41_kgywoL4h56_1.py )

The loop only prints the first row, I think the way I update coordinates of the centre of the image is what's wrong with my code.

if center_d[0] >= WIDTH:
center_s[1] += height
center_d[1] += height


The entire loop's below and if you need more context, visit the link to my program, that i provided above. Thanks!

def draw(canvas):
global deck, cards, WIDTH, HEIGHT
width = 70
height = 106
center_s = [41, 59]
center_d = [41, 59]
for card in deck:
canvas.draw_image(deck_img, center_s, (width, height), center_d, (width, height))
center_s[0] += 70
center_d[0] += 70
if center_d[0] >= WIDTH:
center_s[1] += height
center_d[1] += height

Answer

You forgot about your center_s[0] and center_s[0] coordinates. They are growing constantly. You need to set them to zero, e.g like this:

if center_d[0] >= WIDTH:
    center_s[0] = 41
    center_d[0] = 41
    center_s[1] += height
    center_d[1] += height