[Solved] the continue in for else in python


The latest continue takes effect on the outer for loop, as demonstrated by the following example:

>>> for x in [1, 2, 3]:
...     for y in [4, 5, 6]:
...         print('x =', x, 'y =', y)
...     else:
...         continue
...     print('here')
... 
x = 1 y = 4
x = 1 y = 5
x = 1 y = 6
x = 2 y = 4
x = 2 y = 5
x = 2 y = 6
x = 3 y = 4
x = 3 y = 5
x = 3 y = 6

Note that “here” gets never printed.

Also, note that the inner for loop cannot be continued in any way: the else block is executed when the iterator is exhausted (in my example: when all the ys in [4, 5, 6] have been printed) and when no break statements have been executed. Because the iterator has been exhausted, there are no ways to make it produce more values.

solved the continue in for else in python