Basics · lesson 7 of 10
Parts in this lesson
Awhilerepeat while true
Bfor inrepeat for each value
Crange()a run of whole numbers
Dbreak continuestop, or skip a round
E+=from 1.3, for totals

Repeating things

Last updated

Computers are good at doing the same thing many times without getting bored. In this lesson you'll repeat code while something is true, repeat it a set number of times with range, stop early or skip a round, keep a running total, and fix a loop that stops one round too soon.

By the end of this lesson you can

  • Repeat a block with while, and make sure the loop ends
  • Repeat a block a set number of times with for and range()
  • Leave a loop early with break, or skip a round with continue
  • Count and total values in a loop, and spot an off-by-one mistake

Repeat while it's true: while

while works like if, with one difference: after its block runs, Python goes back and checks the condition again. While it's still True, the block runs again. When it's False, Python carries on after the block.

Press Watch it run and step through. Notice line 4: each round takes one copy away, so the condition on line 2 becomes False after three rounds.

Step 1 code main.py
Edit the code, then run it again

Delete line 4 and press Run. Now copies never changes, so the condition is true forever: an infinite loop. The page stops it after a while and tells you. A while loop always needs something in its block that will eventually make the condition false.

Repeat for each number: for

Often you know how many times to repeat. for day in range(3): runs its block once for each number range(3) gives, and each round, day points at the next one.

range(3) gives 0, 1 and 2: three numbers, starting at 0, the same way positions in text did in lesson 1.5. Watch it run and see day change in the table.

Step 2 code main.py
Edit the code, then run it again

Where range starts and stops

range can take up to three numbers: where to start, where to stop, and how big a step to take. The stop number itself is never included, so range(1, 4) gives 1, 2 and 3. A negative step counts down.

Step 3 code main.py
Edit the code, then run it again

"Up to but not including the stop" is the same rule as slices in lesson 1.5, title[4:10]. It means range(0, n) has exactly n numbers in it, and range(1, n + 1) counts from 1 to n.

Stop early, or skip a round

break leaves the loop straight away, even if there are rounds left. continue skips the rest of this round and goes on to the next one.

This program checks shelves 1 to 7 for a book. Shelf 3 is closed, so it's skipped, and when the book turns up on shelf 6, there's no point checking shelf 7.

Step 4 code main.py
Edit the code, then run it again

Counting and totals

Two patterns appear in loops again and again. A total starts at 0 before the loop, and each round adds to it with +=. A count also starts at 0, and adds 1 when something is true.

Both names are made before the loop. If pages_read = 0 were inside the loop, it would go back to 0 every round. Watch it run and see both values grow.

Step 5 code main.py
Edit the code, then run it again

One round short

This loop should print every day of a 7-day loan, Day 1 to Day 7. Press Run and compare the output with the comment.

Step 6 code main.py
Edit the code, then run it again

This is called an off-by-one mistake, and it's one of the most common bugs in all programming. When a loop runs once too few or too many times, check where it starts and stops first.

Check what you learned

Four questions. Pick or type an answer, then check it.

How many numbers does range(2, 6) give?
What does this program print?
total = 0
for n in range(1, 4):
    total += n
print(total)
Which word leaves a loop straight away?
A while loop runs forever. What's the most likely cause?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python