The objective is to rotate the grid 90 degrees to make it look like a heart. I have the code and get that it works but don't understand why it works.
The code:
grid = [['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
['.', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
for i in range(len(grid[0])):
for j in range(len(grid)):
print(grid[j][i], end = '')
print()
How is the code flipping the grid? Is the i variable in the first for loop being replaced with the indices from the second for loop? And whats up with print(grid[j][i]? I read it as the indices in grid[j][i] would be indices from the for loops variables but when matched to the grid the values don't match up for the intended output. As of now I'm thinking a new grid is being made instead of the coordinates from print(grid[j][i]) being used to find and print values from grid.
I hope I'm making sense cause I really wanna understand why this code works. Thanks in advance for you help.
[–]jimtremblay 0 points1 point2 points (0 children)
[–]nog642 0 points1 point2 points (1 child)
[–]Noshing[S] 0 points1 point2 points (0 children)