KS3 · Computer Science

Iteration: WHILE loops in Python

Delete one line from a friendly little name-checking program and it never stops. Not slows down. Never stops. One line. Let's find out why.

Computer Science · Python

Find Sam: watch the loop think

The user types Andeep, then Laura, then Sam. Step through and watch the condition get checked before every pass.

Before you step: how many times will "Try again, I'm looking for Sam" be printed?

1 name = input("Enter your name: ")
2 while name != "Sam":
3 print("Try again, I'm looking for Sam")
4 name = input("Enter your name: ")
5 print("Hello Sam")
namename != "Sam"
Andeep

Output

 

Step 1: Before the loop even starts, the program asks for a name. Andeep goes into name.

1 / 9

Keep your eye on the while line. It runs before every pass, and the loop only goes in when it says True.

Step 1 of 9: Before the loop even starts, the program asks for a name. Andeep goes into name..

Exam line: When you trace a while loop, check the condition first on every pass, and write down each new value of the variable it tests.
Watch out: The comparison is exact and case-sensitive: "sam" is not equal to "Sam", so typing sam would get you another Try again.

Computer Science · Python

Rebuild the loop, then break it

2 stages are missing. Use the rest of the loop to work out which goes where.

  1. Stage 2: Print "Try again, I'm looking for Sam"

    The first line of the body. It only runs when the check said True.

  2. Stage 4: Go back to the while line

    The body is finished, so the program returns to the top and checks the condition with the new name.

…and stage 4 leads back to stage 1.

Exam line: If a while loop never stops, ask: what inside the loop is supposed to change the condition, and does it?

Two loops that look like twins

while True:vswhile keep_counting:

Both start with while. Only one of them has a switch the program can flip.

Focus

What the condition is

while True:

The value True itself. A fixed value, not a variable.

while keep_counting:

A flag variable, keep_counting, holding True or False.

The insight

This is the whole difference. You can't set True to False, but you can change what keep_counting holds.

Can the condition ever become False?

while True:

No. True is always True.

while keep_counting:

Yes. The program can set keep_counting = False.

The user says they don't want to keep counting

while True:

Nothing happens to the condition. The answer only matters if the program does something with it.

while keep_counting:

The program switches keep_counting to False.

Does the loop ever end?

while True:

It prints 1, 2, 3, 4 and keeps going forever, as long as nothing in the loop changes.

while keep_counting:

Yes. At the next check keep_counting is False, so the loop ends.

Predict, then check

Guess-the-lucky-number: the flag guessed starts as False, and the loop keeps asking while guessed is False.

The user guesses wrong, guesses wrong again, then gets the lucky number. How many times does the loop ask for a guess?

Computer Science · Python

Your turn: unlock the phone

Write a program that keeps asking for the passcode until 1234 is typed, printing Try again after each wrong one, then prints Phone unlocked!

  1. passcode = input("Enter your passcode: ")Ask once before the loop, so the condition has something to test. input() always gives you text.
  2. missing step
Which line is step 2?

Exam line: Read once before the loop, test in the while line, ask again inside the loop, and put the final message after it.

Computer Science · Python

while or for?

For each task, which loop is the clearest choice?

Still to sort

Clearest with a while loop (0)

The number of repeats can change from run to run.

Where the line is: If you can't say in advance how many times it will go round, it belongs here.

Clearest with a for loop (0)

You know exactly how many repeats you need.

Where the line is: A for loop keeps track of the repeats for you, so you don't need a condition that changes.

6 of 6 still to sort.

Both loops repeat. The question is which one fits the job most clearly.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

What you need to know

  • Iteration means repeating a set of instructions. In a program, a loop statement does the repeating.
  • A condition is an expression that works out to either True or False.
  • A while loop is condition-controlled iteration: it repeats its body as long as its condition stays True.
  • The condition gets checked at the start of every pass, before the body runs, never after.
  • Have a goSay the condition comes out False the very first time it's checked. How many times does the loop body run?

    Zero times.

    The check happens before the body, so a False on the first check ends the loop before the body runs even once. It's tempting to say one, but nothing runs first.

  • True? The body runs, then the condition is checked again. False? The loop ends and the program carries on after it.
  • Give the tested value a starting value before the loop, and make sure something inside the loop can change it.
  • Have a goLittle brother's car-trip program: answer = input("Are we there yet? ") then while answer != "yes": print("Not yet!"). He forgot to ask again inside the loop and types no. What happens?

    "Not yet!" prints forever.

    Nothing inside the loop changes answer, so it stays "no" and the condition is True at every single check. Asking again inside the loop is what would let it stop.

  • A Boolean flag is a variable that only holds True or False, like a switch the program flips to stop a loop.
  • while True: uses the fixed value True, not a variable, so it runs forever as long as nothing in the loop changes.
  • Pick while when the number of repeats can change. Know exactly how many? A for loop is usually clearer.
  • Every while line needs a colon at the end, == for 'is equal to', capital T and F, and an indented body.
  • Have a goHere's a while line: while keep_counting == true How many slips can you find, and what are they?

    Two: true needs a capital T, and the colon is missing.

    Python's Boolean values are True and False with capitals, and every while line ends with a colon. The == is fine, because it checks equality.

The big picture

A while loop repeats its body as long as its condition is True, checking it at the start of every pass; when it is False, the loop ends and the program carries on after it. Something inside the loop must be able to change the condition, or it never stops. A Boolean flag is a True/False variable the program can switch to stop a loop, while while True: is fixed, so its condition never turns False. Use while when the number of repeats can change, and for when you know exactly how many.

Key points

1A while loop repeats while its condition is True, and the condition is checked at the start of every pass.
2When the condition is False, the loop ends and the program carries on with the statement after the loop.
3Something inside the loop must be able to change the condition. If the value it tests is never modified, the loop never stops.
4A Boolean flag is a True/False variable the program can switch to stop a loop. while True: is not a flag: True is fixed, so its condition never turns False.
5Use while when the number of repeats can change; use a for loop when you know exactly how many repeats you need.

Worked example

Problem

A treasure-hunt program sets the flag treasure_found to True when distance_travelled is greater than or equal to 250, and the search loop keeps going while treasure_found is False. What happens to the flag and the loop when distance_travelled is 249, then 250, then 300?

⚠ Watch out

Calling while True: a flag. True is a fixed value, not a variable, so its condition never turns False; the loop runs forever as long as nothing in the loop changes. A flag like keep_counting is a variable the program can change.

🧠

Memory hook

Question first, body second. A while loop asks "still True?" before every pass, and something inside has to be able to change the answer.

✓

Check yourself

Your while loop never stops. Does the tested value start before the loop, does a line inside the loop change it, and can that change make the condition False?

Flashcards

(15)
What is iteration?
Repeating a sequence of instructions in a program. A loop statement does the repeating.
What is a condition?
An expression that works out to True or False.
What is condition-controlled iteration, and how do you write it in Python?
Repeating instructions depending on whether a condition is True or False. In Python it's a while loop, which repeats as long as the condition stays True.
When does a while loop check its condition?
At the start of every pass, before the body runs.
What happens when a while loop's condition is False?
The loop ends and the program carries on with the statement after the loop.
What usually makes a while loop never stop?
Nothing inside the loop changes the value the condition tests, so the condition stays True at every check.
What is a Boolean flag?
A variable that only holds True or False. It acts like a switch, and changing it can stop a while loop.
Can a while True: loop ever finish?
Its condition never becomes False, because True is a fixed value. It runs forever as long as nothing in the loop changes. A flag variable such as keep_counting can be switched to False to stop a loop.
When is a for loop usually clearer than a while loop?
When you know exactly how many repeats you need. It keeps track of the repeats for you.
How are the Boolean values written in Python?
True and False, each with a capital first letter.
How can a Boolean flag be tested in a while line?
Directly, as in while keep_counting: or, for the False case, while not guessed:. Comparing with == also works, as in while guessed == False:.
= or == : which one checks whether two values are equal?
== checks equality. A single = stores a value in a variable.
What goes at the end of a while line?
A colon, straight after the condition.
What shows which statements belong inside a while loop?
Indentation. Indented lines under the while line are the loop body.
Why compare input() with "1234" rather than 1234?
input() gives text. Text is never equal to a number, so compare with the text "1234" (or convert the input with int() first).

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

How this lesson was checked. This KS3 Computer Sciencelesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 27 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.

Helpful guides for parents and students