Exercise 5 of 10 · Loops
Countdown
What you will make
A launch sequence that counts itself down from five, then prints a liftoff banner and a small rocket.
The one new idea: A for loop with range() repeats an action
Doing the same thing a set number of times is most of what computers are for — every row of a table, every name on a list, every frame of an animation. A loop is the shortest way to ask for it.
Go straight to the code ↓Repeating a line with for and range
A countdown is one line said over and over with a different number in it each time. You could write five
separate print lines by hand. A loop is how you write the line once and tell Python how many numbers to
feed through it.
The shape looks like this:
foropens the loop.nis a name. Each time round, Python puts the next number into it for you, sonis a variable you never have to set yourself.range(5)is the supply of numbers the loop draws from.- The colon ends the line, and the line underneath is indented. That indent is how Python knows which instructions belong to the loop. Indented lines run once per number. Lines back at the left edge run once, after the loop has finished.
The part that catches everybody
range(5) does not mean one to five. It hands over five numbers beginning at zero: 0, 1, 2, 3, 4. The
number you write is where counting stops, and it is not one of the numbers you get.
Think of a shop open until six. Six o'clock is when it shuts, not another hour of business.
range also accepts three numbers: where to start, where to stop, and how far to move each time round.
range(2, 6)gives 2, 3, 4, 5. It starts at 2 and still stops before 6.range(6, 2, -1)gives 6, 5, 4, 3. The-1means take one off each time, which walks the count downwards. It still stops before 2.
That last rule holds whichever direction you travel: the stopping number is the one number you never see. Counting down, that means it sits one below the last number you actually want printed.
A worked example
for step in range(3):
print("step", step)
print("done")Running that prints four lines:
step 0
step 1
step 2
doneThree numbers, so the indented print ran three times, holding a different value in step on each pass —
0 first, 2 last, no 3 anywhere. The final line is not indented, so it sits outside the loop and prints once
at the end, after the counting is over.
Your turn
The editor holds a launch sequence that already works. Press Run before changing anything and you can see the problem for yourself: it counts 0, 1, 2 and then announces liftoff. No rocket has ever left the ground that way.
One line needs changing, the one with range on it. The banners, the rocket and the print inside the
loop are all finished — leave them alone.
Put three numbers inside range so the count reads 5, 4, 3, 2, 1 and then hands over to the liftoff
banner: where it begins, where it stops, and the step that carries it downwards instead of up.
If something goes wrong
The most likely result is a gap. Both banners print, the rocket prints, and between them there is nothing at all — no countdown and no error message. That happens when the step is missing. Asked to count upwards from 5 until it reaches 0, Python sees that 0 is already behind it, takes no steps, and moves on. There is no complaint because nothing went wrong; the loop simply had nothing to do.
The other likely result is a countdown one number short at one end — starting at 4, or finishing on 2. Both come from the same rule: the stopping number is never printed. If the count ends early, that number is one too high.
Nothing you type here can break anything. The worst case is a countdown that reads oddly, and then you change a number and run it again.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Leaving the step out and writing range(5, 0)
- Python counts upwards unless told otherwise, and there is no way to get from 5 up to 0. The loop takes no turns at all, so nothing appears between the banners — and no error either, because an empty count is not a mistake Python can see.
- Using 1 as the stopping number
- range stops before the number you give it, so the count ends on 2 and the liftoff banner arrives a beat early.
- Expecting range(5) to count 1 to 5
- It starts at zero and stops before five, so you get 0, 1, 2, 3, 4 — five numbers, but not the five you had in mind.
Longer explanation: read the full lesson. Want a blank editor instead? Open the Python playground.