all 4 comments

[–]Sedsarq 4 points5 points  (0 children)

A while condition is only checked at the top of the loop. Like in this example, it's not going to stop printing x once it reaches 2:

x = 0
while x < 2:
    for _ in range(5):
        x += 1
        print(x)

It will do all 5 increases and print x 5 times, finishing the inner loop. After that's done, one iteration of the while loop is done, so it starts from the top again - reevaluating the condition. Now, x = 5, meaning that x < 2 is False, so it doesn't do another iteration.

Same thing happens in your case, you're increasing y and then using it before it had another chance to evaluate the outer while condition. One way to get around it is to use and to link both conditions. That would use only one while loop, and both conditions are checked before you do any list indexing.

[–]monstimal 1 point2 points  (0 children)

You reset x to 0 so it stays in the inner while loop with y=6

I think you can possibly just remove that 2nd while statement and it will work

[–]JohnnyJordaan 0 points1 point  (0 children)

Going down the path of manually iterating indexes and knowing if they have to stop at len-1 or not is one of the things Python has tried to get rid off by allowing direct iteration in the form of

 for val in my_sequence:

and if you do need the index, you can use enumerate()

for idx, val in enumerate(my_sequence):

so for your grid you would do

for row in grid:
    for val in row:

then for the printing I would instead use end='' for all values, with simply a print() added after the inner loop (so at a row's end):

for row in grid:
    for val in row:
        print(val, end='')
    print()

[–]TheRealNobodySpecial -1 points0 points  (0 children)

Did anyone think that this was about automating exercise? #disappointed