To break out of nested (multiple) loops you need to use a variable to keep track of whether you’re trying to exit and check it each time the parent loop occurs.
Example Break in nested loops Python
Simple example code.
is_looping = True for i in range(5): # outer loop for x in range(4): # inner loop if x == 2: is_looping = False print("Inner Loop Break", x) break # break out of the inner loop if not is_looping: print("Outer Loop Break", i) break # break out of outer loop
Do comment if you have any doubts and suggestions on this Python nested loop program.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.