-4

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?

accdias
  • 5,160
  • 3
  • 19
  • 31
  • 2
    Welcome to SO. Please [edit] your question and copy-and-paste the code and error messages as text into your question. Thanks. – 001 Dec 21 '19 at 13:25
  • Also, add all relevant code. It looks like you want a [`while`](https://wiki.python.org/moin/WhileLoop) loop there, but I can't say for sure: `while j >= i and j < 5:` – 001 Dec 21 '19 at 13:29
  • Welcome! Please write your code and error on the post and not by image. You can read about nested loops here https://stackoverflow.com/questions/24591917/nested-loop-python – Matan Shushan Dec 21 '19 at 13:30

1 Answers1

1

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)
>>> 
accdias
  • 5,160
  • 3
  • 19
  • 31