About breaking loop

Which is the best way to break out multiple loops together?
Break function only breaks a single loop...
What would I do in a branching loop situation?
you can use
goto

and return for exit.
First, ordinary logic is helpful.

If, for some reason, the terminating conditions can't be worked out, exceptions are a fall-back plan.

classGetOutOfLoop(Exception):passtry:
done=Falsewhilenot done:
isok=Falsewhilenot(done or isok):
ok = get_input("Is this ok? (y/n)")if ok in("y","Y")or ok in("n","N"):
done=True# probably betterraiseGetOutOfLoop# other stuffexceptGetOutOfLoop:pass
For this specific example, an exception may not be necessary.

On other other hand, we often have "Y", "N" and "Q" options in character-mode applications. For the "Q" option, we want an immediate exit. That's more exceptional.
http://www.keepmequiet.com/2015/12/10-keyboard-shortcuts.html
Topic archived. No new replies allowed.