I am writing a program in Python that uses this nested for loop:
But I get this syntax error:
How should I rewrite this loop to avoid the error?
I am writing a program in Python that uses this nested for loop:
But I get this syntax error:
How should I rewrite this loop to avoid the error?
You can rewrite it to be like this:
def update_grid():
for i in range(5):
for j in range(i, 5):
# do whatever you want with i and j
And here is a proof of concept of the code above:
>>> for i in range(5):
... for j in range(i, 5):
... print((i, j))
...
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(2, 2)
(2, 3)
(2, 4)
(3, 3)
(3, 4)
(4, 4)
>>>