8.5. Using the Loop Variable

In previous pages, we have seen examples that are repetitive, but where each line has a slight change, like this sequence of calls to polygon:

1...
2polygon(mia, 6, 100)
3polygon(mia, 5, 100)
4polygon(mia, 4, 100)
5polygon(mia, 3, 100)

In situations like that, it would be nice to use a loop to repeat the lines instead of typing them out one by one. But to do so, we need to use a variable for the value that changes. Something like polygon(mia, sides, 100) where sides is set to the values 6, 5, 4, 3 in successive calls.

We can do this by making use of the loop variable. It is the variable that is used to store each value in the list that the loop is iterating over. This program shows using two loops. In the first one, the loop variable is called x and is used count from 0 to 4. In the second, the loop variable is called y and counts from 0 to 9.

8.5.1. Range Recipes

That gives us a way to count through a series of numbers. But what if we want, like with the polygon example, to count from 6 down to 3? Or to count from 10 to 100 by 10’s? To do these tasks, we can use a three-parameter version of range:

range(startValue, stopValue, step)

This version of range begins from the startValue, increases by step with each value, and stops when it hits stopValue (but does not actually include the stopValue). If the step is negative, range will count down from startValue to stopValue.

Note

The one-parameter range recipe range(10) is the same as the three-parameter recipe range(0, 10, 1).

Check Your Understanding

8.5.2. Range Recipes With Turtles

We can now use the complex version of range to write a turtle program that repeats steps but uses a different value for a variable during each repetition.

The program below will use the stamp procedure to leave a copy of itself on the screen that will remain after the turtle has moved somewhere else. It also uses the shape procedure to change the turtle’s icon from the default triangle to an actual turtle.

Turtle procedures

The loop, for size in range(5, 60, 2):, says to count by 2’s from 5 to 59. The loop variable is called size and is used to control how far forward the turtle moves in each repetition.

All except one of the shapes you see on the screen here are copies of the turtle shape created by stamp. But the program still only has one turtle instance — can you figure out which one is the real tess? (Hint: if you’re not sure, write a new line of code after the for loop to change tess’ color, or to put her pen down and draw a line, or to change her shape, etc…)

Mixed up program

The following program creates a square spiral as shown below. To do so, we must draw sides of length 0, 5, 10, 15, … 195. After drawing each side, we will turn to get ready for the next side.

Arrange and indent the blocks correctly. You will not use them all.

../_images/TurtleSpiral.png

The following program defines a square procedure and then uses it to draw squares of size 20, 40, 60, 80, and 100.

Arrange and indent the blocks correctly. You will not use them all.

../_images/TurtleSquares.png
You have attempted of activities on this page