Skip to content

Instantly share code, notes, and snippets.

@DixieKorley
Created June 2, 2019 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DixieKorley/ae4d969e1f2dce8b562420e2c4e14e43 to your computer and use it in GitHub Desktop.
Save DixieKorley/ae4d969e1f2dce8b562420e2c4e14e43 to your computer and use it in GitHub Desktop.

Notes from SO

Looping

  1. If you're talking about starting over from the beginning of the for loop, there's no way to do that except "manually", for example by wrapping it in a while loop:
should_restart = True
while should_restart:
  should_restart = False
  for i in range(10):
    print i
    if i == 5:
      should_restart = True
      break

This restarts the loop over and over - anytime it reaches 5, it started over - leads to infinite loop.

  1. How does a while loop actually stop?

A while loop doesn't automatically exit in mid-loop as soon as its condition is no longer true; it just checks the condition at the start of each loop. If you want to get out early, you need to break explicitly (or do something else non-local, like return from the function or raise to an except handler outside the loop).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment