Kids Coding

While Loops

while repeats until a condition is false. Use it for games that keep going.

KidsIntermediateWhile Loops

Repeat until you should stop

for repeats a known number of times. while repeats as long as a check is true.

Count down

n = 3
while n > 0:
    print(n)
    n = n - 1
print("Go")

If you forget to change n, the loop never ends. Always make the check get closer to false.

A game-shaped loop

Keep going while lives remain. This version uses a list of guesses so it can run without typing.

Lives

lives = 3
events = ["miss", "miss", "hit"]
i = 0
while lives > 0 and i < len(events):
    print("Event:", events[i])
    if events[i] == "miss":
        lives = lives - 1
        print("Lives left:", lives)
    else:
        print("Safe")
    i = i + 1

Real life

Filling a water bottle at the tap.

You keep pouring while it is not full. Stop when it is. That is while.

Fill the bottle

ml = 0
while ml < 500:
    ml = ml + 100
    print("Now", ml, "ml")
print("Full. Cap on.")

Change one word. Run it. That is how this idea shows up outside the lesson.

Try it

Exercise 1: Use while to print 1, then 2, then 3.

Show solution
n = 1
while n <= 3:
    print(n)
    n = n + 1

FAQ: While Loops

Common questions about this page.

What is StudyGrid Kids?

StudyGrid Kids is a free coding path for children: Basic, Intermediate, and Advanced. Lessons use Python in the browser. No account and no install.

Should I run while loops examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn while loops in this Kids Coding Python lesson (While Loops).

Which kids level should we start with?

Start at Basic if the child has never written code. Choose Intermediate if they can print, use variables, and write a simple if. Choose Advanced if they already use loops and lists.

Do kids need an account or an app?

No. Kids pages are public. Code runs in the Try Python editor at /try. We do not ask children for a name or an email.

What language do kids learn?

Python, with a first HTML page in Intermediate and Advanced. After Advanced, the main Python tutorial is the next step.

Is the kids coding track free?

Yes. The Kids home, the three levels, and the Python editor on StudyGrid (studygrid.in) are free.