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")
| name | name != "Sam" |
|---|---|
| Andeep |
Output
Step 1: Before the loop even starts, the program asks for a name. Andeep goes into name.
Keep your eye on the while line. It runs before every pass, and the loop only goes in when it says True.
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
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.
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
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?
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 a while loop's condition is False?
What usually makes a while loop never stop?
What is a Boolean flag?
Can a while True: loop ever finish?
When is a for loop usually clearer than a while loop?
How are the Boolean values written in Python?
How can a Boolean flag be tested in a while line?
= or == : which one checks whether two values are equal?
What goes at the end of a while line?
What shows which statements belong inside a while loop?
Why compare input() with "1234" rather than 1234?
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
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.