Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
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
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
polygon(mia, sides, 100) where
sides is set to the values 6, 5, 4, 3 in successive
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(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
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
procedure to change the turtle’s icon from the default triangle to an actual turtle.
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
All except one of the shapes you see on the screen here are copies of the turtle shape
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