Code Bytes · "Intro to Programming" · Episode 11 · Decisions and loops
Infinite loops
An infinite loop is a loop whose condition never turns False, so it never stops. Press Ctrl+C to stop it (Python reports KeyboardInterrupt), and fix it by making sure something inside the loop changes.
The bug that never ends
Last episode, a while loop asked its question before every round and stopped the moment the answer was False. But what if the answer is never False?
Then the loop never stops. That is called an infinite loop. "Infinite" just means endless.
Here's one (on purpose)
``python count = 3 while count > 0: print(count) ``
Read it carefully:
count = 3stores3incount.while count > 0:asks "is count greater than zero?" before every round.- The indented
print(count)shows the number.
But look at what's missing: nothing inside the loop changes count. So count stays 3, and 3 > 0 is True every single time. The output looks like this, and it keeps going:
`` 3 3 3 3 ... ``
How to stop it: Ctrl+C
When a program is stuck in an infinite loop, press Ctrl+C (hold Control, tap C) in the terminal where it is running. This works on Windows, Linux and Mac, and in IDLE too. Python stops the program and prints something like this (this is real output from Python 3.13; your file path will be different):
`` 3 3 Traceback (most recent call last): File "/path/to/loop.py", line 3, in <module> print(count) ~~~~~^^^^^^^ KeyboardInterrupt ``
Don't panic at the word "Traceback". It just tells you where Python was when you stopped it (line 3, the print), and KeyboardInterrupt means "you pressed the stop keys". (Older Python versions leave out the ~~~~~^^^^^^^ line.) Many online editors have a Stop button instead.
The fix: something has to change
The loop needs a line that moves it toward the finish, so the question can turn False:
``python count = 3 while count > 0: print(count) count = count - 1 ``
`` 3 2 1 ``
| Round | count before | count > 0? | prints | count after | |---|---|---|---|---| | 1 | 3 | True | 3 | 2 | | 2 | 2 | True | 2 | 1 | | 3 | 1 | True | 1 | 0 | | check | 0 | False | (loop stops) | |
A quick checklist for every while loop:
- What is the question (the condition)?
- Which line inside the loop changes something the question looks at?
- Does that change move toward making the question
False?
If the answer to 2 is "none", or 3 is "no", you've probably written an infinite loop.
Try it yourself
Open any free online Python editor (search "online Python") and run this. It's already fixed, so it stops by itself:
``python count = 0 while count < 3: print("Tick") count = count + 1 print("Done") ``
You should see:
`` Tick Tick Tick Done ``
Now point at the line that makes the loop stop. Then predict what would change if you wrote count = count + 2 instead, and run it to check (it prints Tick twice, then Done).
Good to know
- Every snippet on this page was run with Python 3 and printed exactly what is shown.
- Sometimes a program is meant to loop forever, for example a game or a server that keeps running until you close it, often written
while True:. This episode is about the accidental kind. - An infinite loop doesn't break your computer. It just keeps one program busy until you stop it.
- If you're unsure whether a loop will end, add a
printinside it to watch the variable change (or not change). - Next episode: functions, how to give a chunk of code a name and reuse it.
Answer to the comment question
How many times does this print hi?
``python n = 1 while n < 5: print("hi") n = n - 1 ``
It never stops: it prints hi forever. It's an infinite loop. n does change, but in the wrong direction: it goes 1, 0, -1, -2, -3 and so on, and every one of those is less than 5, so n < 5 is always True and the condition never turns False. Running it really does print hi again and again until you press Ctrl+C (then Python says KeyboardInterrupt). The trap: something inside the loop changes, but it has to change toward making the question False. Writing n = n + 1 instead would print hi four times.
Test your knowledge
Quick quiz: Infinite loops
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "Compound statements": The while statement: the body repeats as long as the condition is true
- Python Software Foundation, "Built-in Exceptions": KeyboardInterrupt: raised when the user presses the interrupt key, normally Control-C
- Python Software Foundation, "Errors and Exceptions": Handling Exceptions: a user interrupt with Control-C shows up as KeyboardInterrupt
- Python Software Foundation, "An Informal Introduction to Python": First Steps Towards Programming: a while loop whose variable changes each pass
Video transcript
What's the bug that never ends, and how do you stop it?
It's called an infinite loop. Infinite just means endless. It's a loop that never stops.
Here's one. Count equals three. While count is greater than zero, print count. But nothing inside the loop changes count.
So count stays three. Three greater than zero is always True. It prints three, three, three, forever.
The output never ends. To stop it, press Control and C. Python stops the program and says KeyboardInterrupt.
The fix: make sure something inside the loop changes, so the question can turn False. Add count equals count minus one. Now it prints three, two, one, and stops.
How many times does this print hi? Comment your answer below!
That's your byte.
More from "Intro to Programming"
- "Intro to Programming" EP8Comparison and logic
- "Intro to Programming" EP9For loops
- "Intro to Programming" EP13Parameters and return values
- "Intro to Programming" EP14Lists and arrays