Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Infinite while loop

  • while
import random

total = 0
while total >= 0:
    print(total)
    total += random.randrange(20)

print("done")
...
1304774
1304779
1304797
^C1304803
Traceback (most recent call last):
  File "while_infinite.py", line 5, in <module>
    print(total)
KeyboardInterrupt

  • Don't do this!
  • Make sure there is a proper end-condition. (exit-condition)
  • Use Ctrl-C to stop it