Debugging — Python Error Guide

Infinite Loop —
No exception. No crash. Just... never stops.

Your while loop's condition never becomes false, so it just keeps running forever — no traceback, no error message, just a program that hangs. It's the one bug on this hub that doesn't raise an exception at all. Watch a frozen loop variable on LearnBug and see exactly which line should have changed it, but didn't.

No exceptionJust hangs
Logic bugCategory
PythonLanguage

What is an Infinite Loop?

A while loop keeps running as long as its condition evaluates to True. If nothing inside the loop body changes any variable the condition depends on, that condition stays true forever, and the loop never exits — the program appears to "freeze," consuming CPU with no output and no error.

Why visualization helps

There's no traceback to read here — the only way to understand it is to watch the loop's state over multiple iterations and notice what isn't changing. LearnBug lets you step through iterations and see variable values stay frozen, instead of the program just appearing to hang with no information.

LearnBug — a variable's value staying frozen across iterations
🖼 Add a LearnBug screenshot here
What Causes It

Three ways the condition never changes

Forgetting to update the loop variable

A counter that should increment each iteration (like count += 1) is simply missing from the loop body.

Missing increment

Updating the wrong variable

The loop checks while count < 5 but the body updates a differently-named or misspelled variable instead of count.

Wrong variable

A condition that can never become false

while True without any break statement inside, or a condition with faulty logic that's always true regardless of the variable's value.

Faulty condition
Walkthrough

while count < 5 — count never changes

1

count = 0. Check: is 0 < 5? Yes — enter the loop, print 0

This is a completely normal first iteration. Nothing looks wrong yet — the condition check and the print both behave exactly as expected.

count = 0iteration 1
Condition: 0 < 5 = True → loop body runs
2

The loop body prints count, but never increments it

This is the actual bug — the line count += 1 is simply missing. There's no error here, because printing a value is a completely valid operation; it's just not doing what the programmer intended.

count = 0unchanged
Loop body ran fully — count is still 0 at the end of the iteration
3

Check again: is 0 < 5? Still yes — iteration 2 begins, identical to iteration 1

Since nothing changed count, the condition evaluates exactly the same way it did before. This will repeat identically, forever.

count = 0iteration 2, 3, 4... ∞
Every iteration: count = 0, condition always True — no exit possible
4

The program never reaches any code after the loop — it just runs forever

There's no traceback to read because nothing ever raises an exception — the program is simply stuck, consuming CPU indefinitely until manually interrupted.

Result: No error, no output change — the program hangs

Code Example

The broken code — no traceback, it just hangs

PythonBroken — count is never incremented
count = 0
while count < 5:
    print(count)
    # forgot count += 1 — condition never becomes False
# This never prints anything past here. No traceback — it just runs forever.
The Fix

Make sure the loop body actually changes the condition

PythonFixed — count is updated every iteration
count = 0
while count < 5:
    print(count)
    count += 1   # now the condition eventually becomes False
print("Done!")

Quick Reference

SymptomNo output, no crashThe program just hangs — no traceback tells you where to look
Typical causeMissing/wrong variable updateThe condition's variable is never changed inside the loop body
Fix patternEnsure the body updates the conditionEvery path through the loop body should move toward the exit condition
Debugging tipPrint the condition's variablesOr step through with a debugger/LearnBug and watch for a value that stops changing

Frequently asked questions

How do I even notice an infinite loop is happening?

The program stops producing new output, or a script that should finish quickly just keeps running with no end in sight and no increase in CPU-visible progress. In a terminal, Ctrl+C interrupts a hung Python program and shows a KeyboardInterrupt traceback pointing at whichever line was executing — usually right inside the loop.

Is while True: always a sign of a bug?

No — while True combined with an explicit break statement somewhere inside is a completely valid, common pattern (useful when the exit condition is easier to check partway through the loop body than at the top). It only becomes a bug if the break condition can never actually be reached.

Can a for loop have this same problem?

Not in the same way — a for loop iterates over a fixed, already-known sequence (a list, a range, etc.), so it's guaranteed to terminate once that sequence is exhausted. Infinite loops are specifically a while-loop problem, because while's exit condition depends on program state that must be correctly maintained by the programmer.

How can I add a safety net to prevent truly infinite loops during development?

Add a maximum-iteration counter as a temporary safeguard — if iterations > 10000: break — while developing and testing, so a logic bug produces a clear stopping point instead of an indefinite hang. Remove it once you're confident the real exit condition is correct.

Watch your loop's variables — and see what stops changing

Paste your loop into LearnBug and step through iterations to spot the frozen value.

Open Playground →