snippetpythonCriticalCanonical
How can I break out of multiple loops?
Viewed 0 times
howcanmultiplebreakoutloops
Problem
Given the following code (that doesn't work):
Is there a way to make this work? Or do I have do one check to break out of the input loop, then another, more limited, check in the outside loop to break out all together if the user is satisfied?
while True:
# Snip: print out current state
while True:
ok = get_input("Is this ok? (y/n)")
if ok.lower() == "y": break 2 # This doesn't work :(
if ok.lower() == "n": break
# Do more processing with menus and stuffIs there a way to make this work? Or do I have do one check to break out of the input loop, then another, more limited, check in the outside loop to break out all together if the user is satisfied?
Solution
My first instinct would be to refactor the nested loop into a function and use
return to break out.Context
Stack Overflow Q#189645, score: 768
Revisions (0)
No revisions yet.