Your program keeps printing the same number, the terminal will not stop, and everything seems frozen. Python is probably fine. You have likely created an infinite loop.
The most common beginner mistake is simple: the while condition stays True, and nothing inside the loop can make it become False.
The Most Common Mistake
This code is supposed to print 1 through 5:
count = 1
while count <= 5:
print(count)
Instead, it keeps printing 1. The condition count <= 5 is true, but count never changes. Every new check produces the same result.
Fix It by Updating the Control Variable
Add count += 1 inside the loop:
count = 1
while count <= 5:
print(count)
count += 1
Now count eventually becomes 6, making count <= 5 false. The loop stops naturally.
The real rule is not simply “always add one.” Something inside the loop must change the state so that the stopping condition can eventually be reached.
Another Trap: Resetting Inside the Loop
Sometimes an update is present, but the variable is reset on every iteration:
count = 1
while count <= 5:
count = 1
print(count)
count += 1
At the beginning of every iteration, count = 1 sends the value back to its starting point. Initialization usually belongs before the loop, where it runs once. The loop body should move the state forward.
User Input Must Be Updated Too
The same mistake can happen without a counter:
answer = input("Type quit to stop: ")
while answer != "quit":
print("Still running")
If the first answer is not quit, the program never asks again. The value controlling the condition cannot change.
Read new input inside the loop:
answer = input("Type quit to stop: ")
while answer != "quit":
print("Still running")
answer = input("Type quit to stop: ")
The controlling value is now user input instead of a counter, but the principle is exactly the same.
The Three-Question Check
After writing a while loop, ask:
- Condition: what must be true for the loop to continue?
- Update: which line inside the loop changes that condition?
- Stop: when will the condition become false?
Consider this example:
number = 1
while number < 100:
number *= 2
print(number)
The condition is number < 100. The update is number *= 2. The loop stops when number reaches or passes 100.
If you cannot answer one of these three questions, inspect the loop again before running it.
Is while True Always Wrong?
No. while True intentionally creates an endless loop, but break can provide a clear exit:
while True:
answer = input("Type quit to stop: ")
if answer == "quit":
break
This can be useful in interactive programs. Make sure the break is reachable and the exit condition is clear.
Practice
- Explain why
while score < 10: print(score)cannot stop. - Change an input loop so that it stops when the user enters a blank string.
- Challenge: write a
whileloop that counts down from 10 to 1, then identify the line that eventually makes its condition false.
One-Sentence Takeaway
When you write a while loop, find the step inside it that will eventually make the condition false.
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
