I know that similar questions have been posted on this site before, but I truly think this case is different so hear me out.
I have some simple Python code that creates a 2D list of characters (i.e. strings of length 1), tries to process the 2D list, and print it out. Here it is:
def printGrid(image):
for row in range(len(image)):
for col in range(len(image[row])):
print(image[row][col], end='')
print()
print()
def invert(image):
for row in range(len(image)):
for col in range(len(image[row])):
if image[row][col] == '.':
image[row][col] = 'X'
else:
image[row][col] = '.'
grid = [['.'] * 10] * 10 # 2D square of '.'
printGrid(grid)
invert(grid)
printGrid(grid)
I was expecting the code to print out 1 square of .
s and another of X
s, like this:
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
but instead I got 2 squares of .
s, like this:
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
After trying to debug for a while, I found that if I commented out the else
block in the invert
function, then I get the expected output (you can try running the code for yourself to see). This led me to conclude that both the if
block AND the else
block are executed. Does anyone know why this may be happening? (I'm using Python 3.6.1 btw)
P.S. I discovered that this bug apparently only appears for certain grid sizes. For example, if the size of grid
is 5, 7 or 9, then the output is as expected. But if it's something like 6, 8 or 10, then the output is not as expected (apparently the bug only occurs for even-numbers of size?).