-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?

3
  • 2
    Welcome to SO. Please edit your question and copy-and-paste the code and error messages as text into your question. Thanks. Commented Dec 21, 2019 at 13:25
  • Also, add all relevant code. It looks like you want a while loop there, but I can't say for sure: while j >= i and j < 5: Commented Dec 21, 2019 at 13:29
  • Welcome! Please write your code and error on the post and not by image. You can read about nested loops here stackoverflow.com/questions/24591917/nested-loop-python Commented Dec 21, 2019 at 13:30

1 Answer 1

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)
>>> 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that is how I ended up doing it. Thanks!
@MarkMizzi, if possible, please mark my answer as accepted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.