Junior Codes
Clockwork Woods

Python while Loops for Kids

Keep repeating while something is true, and know how to stop.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

while loops, explained simply

A for loop repeats a set number of times. A while loop repeats for as long as a question stays True. It is perfect when you do not know in advance how many repeats you need.

Before each repeat, Python checks the question. If it is True, the indented lines run. If it is False, the loop ends and the program moves on.

Something inside the loop must eventually make the question False, or the loop will never stop. That is called an infinite loop, and every programmer writes one by accident sooner or later.

Example: Count down to launch

countdown = 3
while countdown > 0:
    print(countdown)
    countdown = countdown - 1
print("Lift off!")

Output

3
2
1
Lift off!

Try this

  • Start the countdown at 10.
  • Write a loop that keeps asking "Password?" until the person types the right one.
  • Delete the line that subtracts 1 and see what happens. (Stop the program if it runs forever.)

Practise it in Wonderwild

“Launch the moon balloon”

Mission 14 in Clockwork Woods, one of 36 Python missions in Wonderwild.

Your child writes and runs real Python in the browser while Pip reacts to every line. Nothing to install. For ages 7+.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

Questions parents and kids ask

When should I use while instead of for?+

Use for when you know how many times to repeat or have a list to go through. Use while when you repeat until something happens, like a correct answer or a score reaching a target.

How do I stop an infinite loop?+

In most editors you can press the stop button or Ctrl+C. Then look for the line that should change the loop’s condition.

Prefer learning with a teacher?

Our live Python for Young Developers course teaches these ideas in small online classes led by a software engineer, with projects and feedback. Every live course also includes 12 months of full Wonderwild access for practice.

See the Python for Young Developers course