← All missions

πŸ”„ Never Give Up Loops

πŸ” Repetition & Automation

1) What do I need to do?

🎯 Your mission: Recharge the suit

Start with battery = 0. Use a while loop to add 20 each time and print it, until it reaches 100.

Then print Fully charged!

2) What do I need to know?

while: repeat until it's False

A while loop keeps going as long as its question is True.

Use it when you don't know how many times to repeat β€” like "keep fighting while the villain has health".

⚠️ Make sure something changes inside the loop, or it goes on forever! If that happens, press β–  STOP.

🧰 Your toolkit for this mission

Everything you need to know to finish this lesson on your own:

while loop

Keeps repeating as long as its question is True.

How to write it: while question: then indented lines. Something inside must change, or it never stops!

battery = 0
while battery < 100:
    battery = battery + 20
    print(battery)

After the loop

Lines that are not indented run once the loop has finished.

How to write it: Line them up with the word while.

print("Fully charged!")

Keep asking

Ask again and again until the answer is right.

How to write it: Ask once before the loop, and again inside it.

guess = input("Password? ")
while guess != "zap":
    guess = input("Try again: ")
print("Welcome!")

!= (not equal)

True when two things are different.

How to write it: Great for "keep going while it's wrong".

print("a" != "b")  # True

Think about it

Look at this code. What happens next, and why? Say it in your own words.

while energy > 0:
Watch & explore β–Ά

Power-up: while: repeat until it's False

See it happen

1 / 3
energy = 3
while energy > 0:
3 > 0?
YES
repeat

A while loop checks the question before each turn.

3) Run the code & terminal output

Code Lab 🐍Playground β€” experiment freely!
Hero Terminal

Press β–Ά RUN and your program's messages appear here!

Training Arena
Training Arena
Your superhero companion

Captain Py

Our next adventure is yours to create.