0

I have the following code that doesn't seem to work properly:

from pprint import pprint

sizex = 5
sizey = 5
grid = [[0]*8]*8
cordx = 2
cordy = 3
num = 10

for x in range(sizex):
    for y in range(sizey):
        if grid[cordx+x][cordy+y] != 0:
            grid[cordx+x][cordy+y] = 'x'
        else:
            grid[cordx+x][cordy+y] = num

pprint(grid)

Instead of drawing a square of 10's, and later 'x' starting at grid[2][3] and ending at grid[7][8], it starts drawing at grid[0][3] and goes all the way to grid[8][8], why is that? I can't find the error in my code.

Desired output:

 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

Actual output:

[[0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0],
 [0, 0, 0, 'x', 'x', 'x', 'x', 'x', 0, 0]]
EInherjar
  • 339
  • 6
  • 17

0 Answers0