Python Forum
breaking out of nested loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
breaking out of nested loops
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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
Reply
#3
Maybe put your loops in a function?

if condition:
return

should beam you out of the function!
Skaperen and BashBedlam like this post
Reply
#4
i like the function idea. that would better organize my code, too.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 299 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Openpyxl module breaking my code EnderSM 5 1,055 May-26-2023, 07:26 PM
Last Post: snippsat
  reduce nested for-loops Phaze90 11 1,867 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,707 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,572 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  How to use += after breaking for loop? Frankduc 2 1,483 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  How to fix while loop breaking off raphy11574 3 2,181 Oct-12-2021, 12:56 PM
Last Post: deanhystad
  Break out of nested loops muzikman 11 3,326 Sep-18-2021, 12:59 PM
Last Post: muzikman
  breaking a large program into functions, not acting as expected Zane217 9 3,011 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,357 Sep-04-2021, 04:51 PM
Last Post: imillz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020