1

In the following code, zomPos at [4][7] is the only non-zero thing supposed to be in the array zomPos. However, instead of only zomPos[4][7] being 28, all rows at the 7th index have 28.

turn = 0
zomPos = [[0]*8]*5
zombies = [[0,4,28],[1,1,6],[2,0,10],[2,4,15],[3,2,16],[3,3,13]]
for zombie in zombies:
    if zombie[0] == turn:
        zomPos[zombie[1]][7] = zombie[2]
print(zomPos)

tl;dr:

Code should print:

[[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], 
 [0, 0, 0, 0, 0, 0, 0, 28]]

but prints:

[[0, 0, 0, 0, 0, 0, 0, 28], 
 [0, 0, 0, 0, 0, 0, 0, 28], 
 [0, 0, 0, 0, 0, 0, 0, 28], 
 [0, 0, 0, 0, 0, 0, 0, 28], 
 [0, 0, 0, 0, 0, 0, 0, 28]]
Abdullah
  • 11
  • 1

1 Answers1

0

Just figured it out... zomPos = [[0]*8]*5 creates 5 of the same arrays... zomPos = [ [0] * 8 for t in range(5) ] is what I needed.

Abdullah
  • 11
  • 1