Even shorter programs¶
Let’s get back to the last program of the previous lesson. We needed Karel to take five balls from each of the three squares in fornt of him.
The program that solves the task could look like this:
We see that, in this program, the following group of statements repeats three times:
This allows us to shorten the program further. In explaining the for statement, we mentioned that other loops can be found in the loop body. Now we have the opportunity to use it.
Nested for loops¶
When there is a second loop in the body of one loop, then the first loop is called an outer loop and the other one is called an inner loop. Together we call them nested or inserted loops. In the following example, we’ll see how nested for loops are written.
Pick up five balls three times¶
There are three squares in front of Karel, and there are 5 balls on each of them. Karel needs to pick up all the balls.
The task is repeated, but now we will solve it in a different way.
We mentioned that
i in the previous examples of for statements is a name for the repetition counter. Now, for the first time we need to count other things (balls) during the counting of one thing (squares). This means, for example, that we will need to exactly know when we are on the third square, taking the second ball. Therefore, we can not use the same name for both counters, so we have introduced new names for counters instead of the previous
i. In the following program, we call the square counter
i_square, and the name for the ball counter is
In the given solution, the statement pick_ball() is additionally indented, because it is executed ones for each
i_ball from the range [0, 1, 2, 3, 4]. In addition, the whole statement
for i_ball in range(5): (together with its body and the statement move() above it), repeats 3 times, ones for each
i_square from the range [ 0, 1, 2]. This means that the command pick_ball() executes a total of 3 x 5 = 15 times (on each of the three squares five times).
With nested loops, it is necessary to pay extra attention to the correct indentation of the statements, because it becomes somewhat more complicated. Incorrect indentation of some commands can lead to the wrong result, or to a program that does not work at all.
Tasks for exercise¶
In front of Karel, there is one ball on every third square, and Karel should pick them up both.
Karl should repeat a group of actions “Move three times, and then take the ball” 2 times.
5 balls on every third square¶
There are five balls on every third square in front of Karel, and he should collect them all.
The task is similar to the previous, you just need to repeat picking up the ball. Make sure the loop for taking the balls is underneath the loop for moving forward, not in it.
Karl once more needs to pick up all the balls.
The outer loop should be executed 3 times, and in it Karel should do the following:
Repeat twice the two actions: “move forward” and “take the ball”
We’ve made verbal instructions a little more similar to the program, try converting them into statements. If you still want to see the program itself, click the “Solution” button.
Go about and pick 3 balls each time¶
Write a program by which Karel will pick up all 18 balls.
This task differs from the previous one in just one thing: now picking up the balls should be in an additional loop. This means that we will have three nesting loops: one that counts sides of the maze, one that counts the squares along one side, and the third one that counts the balls on one square.
Again, we give instructions which look like a program (this time without the program itself).