Keep going: while · Robot Quest
How do you repeat something when you don't know how many times?
Course contentsWhy you need it
for i in range(6) needs the number 6. But often you don't know it: "walk until you reach the wall", "keep going until the flag". A while loop repeats as long as a condition is true, so it adapts to any length.
The intuition
"Stir while the soup is still lumpy." You don't count stirs; you check, stir, check, stir... and stop as soon as the check fails.
while not at_goal():
move()Each round Python first asks the question. not flips the answer: not at_goal() is True while Bit is not yet on the flag. When the flag is reached it becomes False and the loop ends.
Danger: if nothing in the body can make the condition False, the loop never ends. Bit stops itself after many steps, and the page stops any run after a few seconds.
Definition
while loop
A while loop repeats its body as long as its condition is True, checking the condition before every round.
Infinite loop
An infinite loop is a loop whose condition never becomes False, so it never stops.
Examples
Walk until the flag
Run it in the lessonChange the corridor length in your head: the code doesn't need to change.
while not at_goal():
move()- L1Check: on the flag? No, so
not at_goal()is True.at_goal() → False - L2Move one square, then go back to the check.
- L1...the check repeats before every move...
- L1On the flag:
not at_goal()is False, the loop ends.at_goal() → True
Two whiles in a row
Run it in the lessonWalk to the wall, turn, walk to the next wall.
while front_clear():
move()
turn_left()
while front_clear():
move()- L1Front clear? Yes: keep moving east.
- L1At the wall the answer is False: the first loop ends.
- L3Turn left to face up.
- L4Second loop: walk up until the wall above the flag.
Common mistakes
A loop that never ends
while not at_goal(): turn_left() never moves, so the condition never changes. Make sure the body moves Bit closer to making the condition False.
Checking only once
The condition is checked before every round, not just the first one.
Quick quiz
1. Where does Bit end up?
while front_clear():
move()Show the answer
Answer: A
Before each move Bit checks the front. When the wall is ahead the condition is False, so the loop stops: no crash.
2. When does a while loop check its condition?
Show the answer
Answer: A
Check, run the body, check again... The loop stops the first time the check is False.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictStop at the gem
Predict the result.
- ModifyAny length
This for loop only works when the corridor is exactly 5 long. Rewrite it with
whileso it works on all 3 maps. - WriteUnknown L
Each map is an L of a different size, with gems along the way. Write one program, with
while, that reaches the flag on all 3 maps with every gem collected.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| while loop | while 循环 | A loop that repeats while its condition is True. |
| Infinite loop | 死循环 | A loop whose condition never becomes False, so it never stops. |
| not | not 取反 | An operator that turns True into False and False into True. |