Python Coding Exercise #45: Fix the Code: Countdown
Part of the series Python Practice Exercise — part 45
Before you start: Know while True and break · Know comparison operators
The question
This program should print out a countdown. The code is as follows:
number = 5
print("Countdown!")
while True:
print(number)
number = number - 1
if number > 0:
break
print("Now!")
This should print out:
Countdown!
5
4
3
2
1
Now!
However, the program doesn’t quite work. Please fix it.
What the broken program does
The program is not broken in a way that makes Python complain. It runs without any error message. It just gives the wrong output:
Countdown!
5
Now!
It prints 5 and then stops, instead of counting down to 1. So this is a logic error: the code is valid, but it doesn’t do what we want. (A similar thing happened with number - 100 in Exercise #29.)
Finding the problem
To find a logic error, I follow the program one round at a time, like Python does. In the first round:
print(number)prints5.number = number - 1makesnumberequal to4.if number > 0:asks whether 4 is greater than 0. It is, so Python runsbreakand leaves the loop.
The break is supposed to stop the countdown when it is finished, but this condition says “stop while there are still numbers left to print”. The if is asking the opposite of what it should.
My Solution
number = 5
print("Countdown!")
while True:
print(number)
number = number - 1
if number == 0:
break
print("Now!")
How My Solution Works
The only change is in the if line: number > 0 became number == 0. Everything else is the same as the broken program.
1. The countdown starts
number = 5 sets the starting number, and print("Countdown!") runs once, before the loop.
2. Each round prints, then subtracts
Inside the loop, print(number) shows the current number, and number = number - 1 makes it one smaller for the next round.
3. break runs only when the number reaches zero
if number == 0: is false while there are still numbers to count, so the loop keeps going. After 1 is printed, number becomes 0, the condition is true, and break ends the loop. Notice that 0 itself is never printed, because the subtraction and the check come after the print().
4. print("Now!") runs once, after the loop
It isn’t indented, so it is outside the loop, as in Exercise #43.
Following the loop
| Round | print(number) | number after subtracting | number == 0? | What happens |
|---|---|---|---|---|
| 1 | 5 | 4 | No | Loop again |
| 2 | 4 | 3 | No | Loop again |
| 3 | 3 | 2 | No | Loop again |
| 4 | 2 | 1 | No | Loop again |
| 5 | 1 | 0 | Yes | break |
After the loop, the program prints Now!.
Things to notice
The break condition says when to stop. With while True, the if before break is the only thing that ends the loop. So its condition should describe the moment the loop is done, not the moment it is still running. In the broken program, number > 0 described “still running”, and in the fixed one, number == 0 describes “done”.
== works here, but <= is safer. The countdown starts at 5 and goes down by exactly 1, so the number is guaranteed to hit 0. But if number started at 0 or lower, number == 0 would never be true after the first subtraction, and the loop would keep printing -1, -2, -3 and never stop. With number <= 0, a start of 0 prints 0 and then Now!. For this exercise, both work, and == is the one I used. In Exercise #43, I also pointed out that a loop which never ends can be stopped with Ctrl + C.
The code is indented by two spaces. The exercise gives the code with two spaces, and that works, because Python only needs the lines inside the loop to be indented by the same amount. Four spaces is more common, as used in Exercise #44.
Key takeaways
- A logic error runs without any error message, but gives the wrong output. Following the code one round at a time helps to find it.
- When a loop uses
while True, theifbeforebreakshould describe when the loop is finished. - A condition that is the wrong way round can be fixed with a small change, such as
>to==. ==is fine when the number is sure to reach the value exactly, but<=is safer if the number could start at or below it.- Subtracting after printing means the final value,
0here, is never printed.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.