Nex2School

Parent guide · 7 min read

Python while loops for kids: repeating until something changes, and escaping the infinite loop

A for loop repeats a set number of times. A while loop repeats for as long as something stays true, which makes it the natural choice for "keep asking until the answer is right" or "keep going until the score hits 10". It is also where almost every child writes their first accidental infinite loop, a program that never stops and fills the screen. That is a normal rite of passage rather than a sign something is wrong. This guide explains how a while loop decides to keep going, traces one line by line, and shows how to stop a runaway program calmly.

Try this tonight

4 things you can do at home

  1. Ask "what are we waiting for?"

    Before any code, have your child finish the sentence "keep going until ...". Until the countdown reaches zero, until the password is correct, until the savings reach 50. The thing being waited for becomes the condition, and the thing that moves it forward becomes the line inside the loop.

  2. Write the countdown first

    Start with count = 5, then while count > 0: print(count) and count = count - 1 on the next indented line. Run it and watch it print 5, 4, 3, 2, 1. A countdown is small enough to predict in the head and it has exactly the three parts every while loop needs: a start, a test and a change.

  3. Break it on purpose, safely

    Delete the count = count - 1 line and run it again, with your child told in advance how to stop it (Ctrl+C in most terminals, or the stop button in a coding app). Seeing the infinite loop under control removes the fear, and your child learns to recognise the cause: nothing inside the loop changes the condition.

  4. Trace the values in a table

    Draw columns for "count", "count > 0?" and "printed". Fill in one row per check, including the final check that comes out false. The last row, where the test fails and the loop exits without printing, is the one beginners most often forget.

  1. How a while loop decides

    The shape is the word while, a condition, a colon, then indented lines. Python checks the condition first. If it is true, it runs every indented line and goes back to check again. If it is false, it skips the loop completely and carries on with the first line at the left edge. The check happens before each repeat, so a loop whose condition is false from the start runs zero times.

    This is the main difference from a for loop. A for loop walks through a sequence and ends when the sequence runs out. A while loop has no sequence, only a question asked over and over, which is why the programmer is responsible for making sure the answer eventually changes.

  2. A worked example: saving up

    Suppose a child has saved 12 and saves 5 each week, and wants to know how many weeks it takes to reach 50. Write savings = 12 and weeks = 0, then while savings < 50: savings = savings + 5 and weeks = weeks + 1. Trace it. After week 1 the savings are 17, after week 2 they are 22, then 27, 32, 37, 42, 47 and finally 52 after week 8. At 52 the test savings < 50 is false and the loop stops, so weeks is 8.

    The child cannot know in advance how many repeats this needs without doing the sum, which is exactly when a while loop is the right tool and a for loop is not. Check the answer by hand: 12 + 8 × 5 = 52, and 12 + 7 × 5 = 47, which is still under 50.

  3. The infinite loop and how to stop it

    An infinite loop happens when the condition can never become false. The usual causes are forgetting the line that changes the variable, changing it in the wrong direction (adding when the loop needs to subtract), or writing the test so it is always true, such as while count > 0 when count is going up.

    When it happens, do not close the whole window in a panic. In most terminals Ctrl+C sends an interrupt that stops the program and prints where it was. Then read the loop with your child and ask three questions: what is the condition, what changes it, and does that change move it toward false. The answer is nearly always in the second question.

  4. Keep asking until it is right

    Another classic use is checking input. Start with answer = "" and write while answer != "blue": answer = input("Favourite colour of the sky? "). The loop keeps asking until the typed word matches exactly. Trace it for the inputs "red", "Blue" and "blue": the first two are not equal to "blue" (capital letters count), so it asks again, and the third ends the loop.

    This example shows why a while loop is useful for real programs: games that run until the player quits, quizzes that repeat until a correct answer, and menus that return until the user chooses exit. It is also a good moment to point out that the starting value matters, because answer has to exist before the first check.

Signs it’s time for outside help

When home help has done what it can

  • Writes a while loop without any line inside it that changes the condition.
  • Panics or closes the window when a program will not stop.
  • Cannot say when the condition is checked, before or after the indented lines.
  • Mixes up when to use a for loop and when to use a while loop.
  • Forgets to give the variable a starting value before the loop.
  • Can copy a working loop but cannot change the condition to do something new.

Questions parents ask

FAQ

What is the difference between a for loop and a while loop?

A for loop repeats once for each item in a sequence, so the number of repeats is known when it starts. A while loop repeats as long as a condition is true, so it suits situations where you do not know in advance how many repeats are needed, such as waiting for a correct answer.

How do I stop an infinite loop in Python?

Press Ctrl+C in the terminal where the program is running. In a coding app or editor there is usually a stop button. Then check that something inside the loop changes the variable in the condition, and in the right direction.

Does Python have a do-while loop?

No. Python has only for and while loops. People who want "run once, then check" usually use while True with a break statement when the condition is met. This is an idea to leave until the plain while loop is comfortable.

Can a while loop run zero times?

Yes. The condition is checked before the first repeat, so if it is false at the start, the indented lines never run. For example, count = 0 followed by while count > 0 does nothing.

What age can children learn while loops?

Many children meet while loops a little after for loops, often around ages 9 to 11 in a beginner Python course, and some earlier. Comfort with variables and with reading a true-or-false comparison matters more than a particular age.

Not sure which is right for your child?

Tell us their age and what they have tried so far, and we will suggest where to start before you book anything.

Keep reading