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
Output
Step 1: The program asks for a name and the user types Andeep. Now name holds Andeep.
Press Next and keep your eye on the True or False value in the Variables panel.
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
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.
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
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?
What is a condition?
What is condition-controlled iteration, and how do you write it in Python?
When does a while loop check its condition?
What happens when the condition of a while loop is False?
Why does a loop never stop if nothing inside it changes the variable it checks?
What is a Boolean flag?
Why is while True: not a flag?
When should you choose a for loop instead of a while loop?
How do you write the Boolean values in Python?
Which symbol checks whether two values are equal?
What goes at the end of a while line?
What shows which statements belong inside the while block?
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 postedMore KS3 Computer Science topics
- Abstraction in computational thinking
- Adding binary numbers
- Binary to denary conversion
- Boolean logic: AND, OR, NOT
- Bubble sort
- Building truth tables
- Client-server vs peer-to-peer
- Collecting and recording data
- Comparing sorting algorithms
- Compressing data
- Creating a 3D animation
- Decomposition: splitting problems up
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.