Python Forum

Full Version: breaking out of nested loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
deep in some nested loops and a condition test, i need to break out of all of those loops. is there an easy way to do this? i can't use goto. what about a faked try/except?
Using an exception will work, but is a kludge and can end up causing bigger problems down the road, esp with maintenance.
Another, again not great idea, is to use a flag that you test as you exit the deeper loops to see if you should break

I'd look at how you could restructure your code
Maybe put your loops in a function?

if condition:
return

should beam you out of the function!
i like the function idea. that would better organize my code, too.