I have this piece of code here that generates a 2D array and fills it with ” “, asks the user to enter an x and y coordinate, and then changes that coordinate to a “1”. When I try to change the element, it seems that python uses the x variable as the array’s column coordinate and the y variable as the array’s row coordinate instead of vice versa. The output shows that the columns and rows are flipped

Here is the code:

tm = [] for i in range(6): row = [] for j in range(6): row.append(" ") tm.append(row) x = int(input("x: ")) y = int(input("y: ")) tm[x][y] = "1" for row in tm: print(row)

And Here’s the output:

>>>x: 4 >>>y: 5 >>>[' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', '1'] [' ', ' ', ' ', ' ', ' ', ' ']

Thanks in advance to any clever person out there that can figure this out!

## Answer

If you consider `tm[x]`

to be a row (as you do when looping and printing), then that means you’re treating `x`

as a vertical coordinate, since rows are stacked vertically. Therefore, in `tm[x][y]`

, `y`

is referring to a particular column (i.e. horizontal position) in that row. You just have your coordinates backwards. `tm[y][x]`

will work how you expect.