KS3 · Computer Science

Iteration: WHILE loops in Python

Delete one line from a friendly name-greeting program and it never stops. Just one line. Let's find out why.

Computer Science · Python

Be the program: find Sam

The user types Andeep, then Laura, then Sam. Step through and watch the condition get checked before anything inside the loop runs.

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

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

Variables

name=Andeep

Output

 

Step 1: The program asks for a name and the user types Andeep. Now name holds Andeep.

1 / 9

Press Next and keep your eye on the True or False value in the Variables panel.

Step 1 of 9: The program asks for a name and the user types Andeep. Now name holds Andeep..

Watch out: The question comes FIRST on every pass. Spot the moment the answer flips to False.

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 1: Check the condition

    name != "Sam": True or False? If it is False you leave the loop here and the program carries on after it.

  2. Stage 2: Condition is True: run the loop body

    Only gets here while the condition is True.

…and stage 4 leads back to stage 1.

Two loops that look alike

while True:vswhile keep_counting:

Both start with the word while. Only one of them can ever be switched off.

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. True never changes. A flag is a variable, so it can.

Can anything make it False?

while True:

No. True is not a variable, so nothing can switch it off

while keep_counting:

Yes. The program can set keep_counting to False

User says they do not want to keep counting

while True:

There is no flag for the answer to switch off, so the loop carries on

while keep_counting:

The program switches keep_counting to False

Does the loop ever end?

while True:

No. It runs forever on its own, printing 1, 2, 3, 4 and carrying on

while keep_counting:

Yes. Once keep_counting is False the condition is False and the loop ends

Predict, then check

In the guess-the-lucky-number game, the flag guessed starts as False and the loop keeps asking while guessed is False.

The user guesses wrongly twice, then guesses the lucky number correctly. How many times does the loop ask for a guess?

Computer Science · Python

Your turn: unlock the phone

Finish this program so it keeps asking for the passcode while the passcode entered is not the correct one, and prints "Phone unlocked!" once it is. The correct passcode is 1234.

  1. passcode = input("Enter your passcode: ")Ask for the passcode first.
  2. missing step
Which line is step 2?

Computer Science · Python

While loop or for loop?

For each task, decide which loop fits, then read why.

Still to sort

while loop (0)

The number of repeats can change.

Where the line is: You can't say in advance how many times it will repeat.

for loop (0)

You know exactly how many repeats are needed.

Where the line is: The exact number of repeats is known before the loop starts.

6 of 6 still to sort.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

What you need to know

  • Repeating instructions in a program is called iteration, and a loop statement does the repeating.
  • A condition is an expression that works out to True or False.
  • In Python, a while loop repeats its instructions for as long as the condition stays True.
  • The condition is checked at the start of every pass, before the loop body runs.
  • Have a goSay the condition comes out False the very first time it is checked. How many times does the loop body run?

    Zero times.

    The check comes before the body. A False result ends the loop straight away and the program carries on after it.

  • If it is True, the body runs and the condition gets checked again.
  • If it is False, the loop ends and the program carries on with the statement after the loop.
  • Something inside the loop has to be able to change the thing the condition checks, or the loop never stops.
  • A Boolean flag is a variable with just two values, True or False. Changing it can stop a while loop.
  • Have a goA loop is controlled by the flag keep_counting. The user says they do not want to keep counting. What should the program do to the flag?

    Switch keep_counting to False.

    The loop's condition is the flag, so changing the flag to False makes the condition False and the loop ends.

  • A loop that starts with while True: runs forever on its own, because True is a fixed value, not a variable.
  • Use a while loop when the number of repeats can change. Use a for loop when you know the exact number.
  • Four things to check in any while loop: capital T and F, == to test equal, a colon, and indentation.
  • Have a goSpot the slip: while keep_counting == true: — which word is wrong?

    true. It needs a capital T: True.

    Python's Boolean values are written True and False, with a capital first letter, so lowercase true is not the value.

The big picture

A while loop repeats its instructions for as long as its condition is True. The condition is checked at the start of every pass, and the loop ends when it is False. So something inside the loop must be able to make the condition False, or the loop never stops. A Boolean flag variable such as keep_counting is one way to switch a loop off, but while True: is a fixed value that runs forever on its own.

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.
3A loop whose condition never becomes False never stops, so something inside must be able to change it.
4while True: runs forever on its own. A flag variable such as keep_counting is what can be switched to False.
5Pick while when the number of repeats can change, and for when the exact number is known in advance.

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 if distance_travelled is 249, then 250, then 300?

⚠ Watch out

Thinking while True: is a flag the program can switch off. True is a fixed value, so on its own it runs forever. It is a flag VARIABLE such as keep_counting that can be set to False to stop a loop.

🧠

Memory hook

Question first, body second. A while loop asks "still True?" before every pass, and only a False lets you out.

✓

Check yourself

A friend's while loop never stops. Without looking back, say what you would check first, and what they would need to add inside the loop.

Flashcards

(13)
What is iteration?
Repeating a sequence of instructions within a program. A loop statement can be used to do the repeating.
What is a condition?
An expression that evaluates to True or False.
What is condition-controlled iteration, and how do you write it in Python?
Repeating instructions based on whether a condition is True or False. In Python it is a while loop, which repeats as long as the condition remains true.
When does a while loop check its condition?
At the start of every pass, before the loop body runs.
What happens when the condition of a while loop is False?
The loop ends and the program carries on with the statement after the loop.
Why does a loop never stop if nothing inside it changes the variable it checks?
The value never changes, so the condition stays True every time it is checked.
What is a Boolean flag?
A variable that acts like a switch and only has two values, True or False. Changing it can stop a while loop.
Why is while True: not a flag?
True is a fixed value, not a variable, so nothing can switch it off. On its own the loop runs forever.
When should you choose a for loop instead of a while loop?
When you know exactly how many repeats are needed. A for loop tracks the number of repeats as part of its operation.
How do you write the Boolean values in Python?
True and False, each with a capital first letter.
Which symbol checks whether two values are equal?
A double equals symbol, ==.
What goes at the end of a while line?
A colon, straight after the condition.
What shows which statements belong inside the while block?
Indentation.

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 9 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.