Make a Tiny Game
beginner25 XP

Rocket Countdown

Count down to liftoff with a while loop.

Rocket Countdown 🚀

10... 9... 8... Every rocket launch has a countdown. Let's code one!

A while loop keeps repeating as long as something is true:

Python
count = 5

while count > 0:
    print(count)
    count = count - 1

print("Liftoff!")

What happens:

  1. count starts at 5. Is 5 > 0? Yes, so it prints 5 and makes count 4.
  2. ...again and again...
  3. When count reaches 0, count > 0 is false, so the loop stops.
  4. Then Liftoff! 🚀
💡

⚠️ If you forget count = count - 1, count stays 5 forever, and the loop never stops! That's called an infinite loop.

for or while?

  • for loops when you know how many times: "plant 3 trees".
  • while loops when you repeat until something changes: "keep counting down until zero".

🛰️ Saving fuel

Python
fuel = 100

while fuel > 0:
    print("Fuel left:", fuel)
    fuel = fuel - 25

print("Out of fuel - landing!")

🌌 Fun fact

Rockets need to go about 28,000 km per hour to stay in orbit around Earth. That's fast enough to cross the USA in about 10 minutes!

⌨️

Type it yourself

Don't just read it — type the example from above into the box, press Run ▶, and watch what happens. Typing it yourself is how it really sticks! Stuck? Tap Show example.

Python · Playground
Output
Press ▶ Run to see your code come to life…

Quick check

01/5
When does a while loop stop?