Python while Loops Explained: Repeat Until Something Changes

A loop that runs until a condition stops being True — and the trap of forgetting to update it.

  • Lesson 1 of 5
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

while — repeat until something changes

  1. The dragon: for loops repeat a NUMBER of times, decided in advance. while loops are different — they repeat UNTIL something changes.

  2. The dragon: while CONDITION: keeps going as long as the condition is True. No fixed count anywhere.

    example.pyPython 3
    patience = 3while patience > 0:    print("Pacing...")    patience = patience - 1
    OutputPacing... Pacing... Pacing...
  3. The dragon: Every pass checks the condition FRESH. The moment patience hits 0, the loop stops — nobody tells it '3 times', it just keeps going until False.

    example.pyPython 3
    patience = 3while patience > 0:    print("Pacing...")    patience = patience - 1
  4. The dragon: Here's the trap: if nothing inside the loop ever changes patience, the condition is ALWAYS True. The loop never stops.

    example.pyPython 3
    patience = 3while patience > 0:    print("Pacing...")
  5. The dragon: If that happens for real, you'll see a message about your code getting stuck in a loop, and it reboots automatically. That's not a crash — it's the game catching exactly this.

  6. The dragon: The condition can be a question about the world, too. while front_is_clear(): keeps me walking until something blocks the way — however long the tunnel is.

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 1

    patience = 3, and the loop body never changes patience. What happens?

    question.pyPython 3
    patience = 3while patience > 0:    print("Pacing...")
    • It repeats forever — patience > 0 never becomes FalseCorrect
    • It runs exactly 3 times, then stops on its own
    • It causes an error immediately, before running at all

    Why: Nothing inside the loop ever changes patience, so patience > 0 stays True forever, and the loop never ends. This is the single most common while-loop mistake — always ask yourself: what, inside this loop, actually changes the condition?

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    The dragon paces back and forth while its patience is above 0. Patience starts at 3 and drops by 1 each pace. Print 'Pacing...' each time it paces.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.