Skip to main content
Programming from zero

Count down before a restart

easyLoops

Problem statement

A for loop is the right tool when you know exactly how many times to repeat. A while loop is the right tool when you want to repeat "as long as some condition holds," which is a better fit for a countdown that stops the moment it reaches zero.

Common mistake: forgetting to update the counter inside the loop.

TEXT
Wrong:
count = 5
while count > 0:
print(count)
# count never decreases here

Without something inside the loop that changes count, the condition count > 0 is always true, and the loop runs forever. Every while loop needs a line that moves it closer to becoming false.

Your task: starting at count = 5, print each number counting down to 1, then print Restarting...

Approach

Print the current count, then decrease it by 1, and keep going while it's still above zero:

count = 5
while count > 0:
    print(count)
    count -= 1
print("Restarting...")