Learning LibraryCore Coding LibraryTeens

While Loops in Python: repeat until a condition breaks

When you know the count, use a for loop. When you don't — repeat until something happens — you use a while loop. Every game's frame loop is a while loop at heart.

The big idea

A while loop re-runs its body as long as its condition stays true, re-checking the condition before each pass.

See it in code

1The basics

At its core, a while loop re-checks a condition and runs its body again while that condition holds. A countdown is the plainest case — repeat until the counter hits zero:

python
countdown = 3
while countdown > 0:
    print("Launching in", countdown)
    countdown -= 1

print("Go!")
Run it — the loop runs while countdown is still above zero:
Launching in 3
Launching in 2
Launching in 1
Go!

The body decrements countdown each pass. The moment countdown > 0 becomes false, the loop exits — no fixed trip count, just a condition.

2A step further

Now model the exact loop we're about to run in the game. An enemy starts at the top and drops a fixed amount each frame; we keep looping while it's still above the bottom, counting frames as we go:

python
y = 0
frames = 0
while y < 600:
    y += 150
    frames += 1

print("Frames to reach the bottom:", frames)
Run it — the loop counts frames until y crosses the bottom:
Frames to reach the bottom: 4

This is the whole idea in plain numbers: a value (y) that changes each pass, and a condition (y < 600) that eventually breaks. Swap the raw y for a real sprite and you get the version below.

3In our world

Same loop, real objects. We advance one enemy down the screen a frame at a time, counting frames, and run while it hasn't reached the bottom — the moment it does, the loop ends:

python
from game import Stage, Enemy

screen = Stage.create()
Stage.clear(screen)

enemy = Enemy(240, 90)
frames = 0
while not enemy.at_bottom():
    enemy.move_down()
    frames += 1

print("Frames to reach the bottom:", frames)
enemy.draw(screen)
Run it — the loop steps the enemy down until it lands:
Frames to reach the bottom: 470
A single purple enemy sprite resting near the bottom of a dark play area.

It's the plain-number loop with enemy.at_bottom() in place of y < 600. Because the enemy's y changes every pass, the condition eventually flips and the loop exits. The real frame loop is the same shape: while running: — update, draw, repeat.

The same idea, everywhere

While loops fit any 'until' problem: keep hashing until a nonce meets the target, keep iterating until a value converges, read lines until the file ends, retry until a request succeeds. Whenever the number of repeats depends on what happens inside the loop, while is the tool.

Try it yourself

Give the enemy speed = 2 (set enemy.speed = 2) and predict how the frame count halves. Then add a break that stops the loop early if frames passes 200, to see how you bail out mid-loop.

The common mistake

The infinite loop. If the condition can never become false — you forget enemy.move_down(), so at_bottom() stays false forever — the program hangs. Always make sure the loop body moves the condition toward ending.

What it unlocks

While loops rely on conditionals and boolean logic, and are the backbone of the game loop.

Want the simpler version? Read the Kids version →