Breaking out of nested while True loop


I am running a while True: loop in a webscraping script. I want the scraper to run in an incremental loop until running into a certain error. The general question is about how to break out of a while True loop when a certain condition is matched. The code as is just keeps on outputting the first run forever:

output 1;1 ... output 1;n

This is a minimal reproducible example of my code.

runs = [1,2,3] for r in runs: go = 0 while True: go +=1 output = ("output " + str(r) + ";" +str(go)) try: print(output) except go > 3: break

The desired output is:

output 1;1 output 1;2 output 1;3 output 2;1 output 2;2 output 3;3 output 3;1 output 3;2 output 3;3 [done]

You don't need try and except here. Keep things simple and just use a simple while condition on your go variable. In that case, you don't even need a break because as soon as go>=3, the condition will be False, you will come out of the while loop and restart the while loop for the next value of r.

runs = [1,2,3] for r in runs: go = 0 while go <3: go +=1 output = ("output " + str(r) + ";" +str(go)) print(output)


output 1;1 output 1;2 output 1;3 output 2;1 output 2;2 output 2;3 output 3;1 output 3;2 output 3;3

Alternative to while : As suggested by @chepner, you don't even need while and are better off with a for loop over go as

for r in runs: for go in range(1, 4): output = ("output " + str(r) + ";" +str(go)) print(output)



  • Adding a user to a group Django Rest Framework
  • Python Moviepy installation problems (windows 7x64)
  • Relationship spanning four tables in SQLAlchemy
  • Generate unique ID to share with multiple tables SQL 2008
  • Number formatting as-you-type in an input field in Angular2 with double binding
  • Which tool for colorizing output of javac?
  • How to install docker in docker container?
  • SpringDataRest and show welcome-file-list/disable link discovery
  • layer popup with jQuery
  • Reduce the memory used loading huge pandas dataframe from MongoDB
  • python & pandas- Calculation bewteen rows based on certain values in columns from DataFrame
  • Spring Security sec:authorize tag using Java Config
  • jre_home environment variable is not defined correctly while starting tomcat
  • Using an interface to apply method to ArrayList
  • exception thrown while building the java application using netbeans
  • Problem with installing Charm-Crypto for Python3
  • get all files in git diff in intellij
  • Multiple git user in single device
  • Wrap array elements in divs based on same value
  • ggplot2 facet_grid with distinct x-axis labels using facet_grid
  • Hyperlink to Outlook Attachment
  • What's the difference between using RDFS/OWL and XML?
  • Sum children values and save result to parent in n-ary tree in Javascript
  • How to smoothly connect two signals in matlab [closed]
  • SELECT on JSONField with Django
  • Is possible having two COM STA instances of the same component?
  • How does the dispatcher work when mixing sync/async with serial/concurrent queue?
  • Circular Left Rotation Algorithm in C#
  • What does “T extends Junk” mean in a generic class in Java?
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • Create an Office365 mailbox from within C# Web API method
  • Separating definition/instantiation of template classes without 'extern'
  • gnuplot - How to make zmin equal to zmax keeeping autoscale on z axis
  • Terminal run dalvikvm with am.jar
  • How to handle div that is created dynamically in a table
  • Make checkout phone field optional for specific countries in WooCommerce
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • media foundation H264 decoder not working properly
  • ReferenceError: TextEncoder is not defined