Loops inside loops · Robot Quest
What if the repeating part itself contains a repeat?
Course contentsWhy you need it
Many tasks repeat at two levels: three rows, and in each row, four seats; four sides of a square, and on each side, several steps. A nested loop (a loop inside another loop) describes exactly this.
The intuition
A clock: the minute hand goes round 60 times for every one turn of the hour hand. The inner loop finishes completely before the outer loop moves on.
for side in range(4): # outer: 4 sides
for step in range(3): # inner: 3 steps per side
move()
turn_right() # once per sidemove() runs 4 × 3 = 12 times. turn_right() belongs to the outer loop (it is indented once), so it runs 4 times. Indentation tells you which loop a line belongs to.
Definition
Nested loop
A nested loop is a loop placed inside the body of another loop; the inner loop runs completely for each round of the outer loop.
Examples
Three groups of three
Run it in the lessonThe outer loop counts groups; the inner loop handles the gems in one group.
for group in range(3):
for gem in range(3):
move()
pick()
move()- L1Outer round 1 (group = 0).
group = 0 - L3The inner loop runs 3 full rounds: move, pick, three times.
gem = 0, 1, 2 - L5Back in the outer body: one move over the empty square.
group = 0 - L1Outer round 2 starts the inner loop again from gem = 0.
group = 1 - •After 3 outer rounds: 9 gems picked and Bit is on the flag.
Around the pillar
Run it in the lessonThree sides, two steps each, a right turn after every side.
for side in range(3):
for step in range(2):
move()
turn_right()- L3Side 1: two moves east.
- L4Turn right once, at the end of the side.
- L3Side 2: two moves south.
- L3Side 3: two moves west, onto the flag.
Common mistakes
Wrong indentation level
A line indented 8 spaces is in the inner loop; 4 spaces is in the outer loop. Moving a line one level changes how often it runs.
Reusing the same counter name
Give the two loops different names (side, step). Using i for both is confusing and causes bugs later when you use the counters.
Quick quiz
1. How many times does move() run?
for a in range(2):
for b in range(3):
move()Show the answer
Answer: B
The inner loop runs 3 times for each of the 2 outer rounds: 2 × 3 = 6.
2. Where does Bit end up?
for side in range(3):
for step in range(2):
move()
turn_right()Show the answer
Answer: B
turn_right() is inside the inner loop, so Bit turns after every step and walks into the pillar.
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.
- PredictTwo sides
Predict where Bit ends up.
- ModifyBigger groups
The new map has 2 groups of 4 gems. Change the numbers so Bit collects them all and reaches the flag.
- WriteGem ring
Collect all 9 gems around the block, then step onto the flag. Use a nested loop, at most 6 lines.
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 |
|---|---|---|
| Nested loop | 嵌套循环 | A loop inside the body of another loop. |
| Inner loop | 内层循环 | The loop inside; it runs completely for each round of the outer loop. |
| Outer loop | 外层循环 | The loop that contains another loop. |