Python While Loops
while keeps going only while a question still answers True. It asks that question before the indented lines, every time, including the first time. The for loop on the previous page walked a row it already had: 0, 1, 2 from range, or the letters of tea. while does not carry a row. It carries a question, and you have to change something inside the loop or the question never turns False.
Start cups at 0. The question is cups < 3. Zero is less than 3, so the indented lines run: print writes 0, then cups = cups + 1 stores 1. while asks again. 1 is still less than 3, so you see 1, and cups becomes 2. One more pass writes 2, and cups becomes 3. Now the question is False, so the indented lines stay quiet. You never see 3. The 3 is sitting in cups, which is exactly why the question failed.
cups = 0
while cups < 3:
print(cups)
cups = cups + 1
Read the screen as 0, then 1, then 2. After those three lines, cups holds 3. If you printed cups once more, level with while, you would see 3. The loop itself does not print that 3, because the question is asked before print.
Already too big, so the indented lines never run
Set cups to 3 before while. The first question is already False. Nothing indented runs, not even once. print("done") is level with while, so it is not part of the loop. It runs once, after while has decided to stop.
cups = 3
while cups < 3:
print(cups)
cups = cups + 1
print("done")
The only line on the screen is done. There is no 3, because print(cups) lives inside the loop and the loop never started.
cups = cups + 1 is the line that lets a later question fail. Take it away and cups stays 0. cups < 3 stays True. while asks, prints 0, asks again, prints 0, and never finds a reason to stop. The program does not finish that loop.
while needs a colon, the same way for and if do. The lines that repeat sit four spaces further in. The word is lowercase. While with a capital W is a name Python does not know, and the program stops.
The next page can stop early
This page waits until the question itself becomes False. The next page is about breaking the loop, or skipping a single pass, before that question has failed.
Guess, then look
- cups starts at 0 and the question is cups < 3. Which numbers are written?
- After that loop, what number is stored in cups, even though it was not written?
- cups starts at 3, and done sits level with while. What appears?
- You delete cups = cups + 1. Does the loop finish?
Where this goes wrong
- Treating the final 3 as one of the printed lines
- Expecting a number when cups is already 3 before while
- Forgetting cups = cups + 1 and waiting for a last line that never comes
- Dropping the colon after while
- Writing While with a capital W
What to carry out
while asks before every pass. The indented lines run only while that answer is True.
From 0, with cups < 3, you see 0, 1, and 2. cups is then 3, and that 3 is not printed.
If the question is already False, those indented lines never run, and a line level with while still runs once.
Without cups = cups + 1 the question stays True and the loop does not finish.
The next page can break the loop or skip one pass.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top