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") # TrueThink 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 / 3energy = 3 while energy > 0:
A while loop checks the question before each turn.
3) Run the code & terminal output
Press βΆ RUN and your program's messages appear here!


Captain Py
Our next adventure is yours to create.
