8.1. Repeating Steps & Range¶
forloop to repeat steps with turtles.
Often times we have to repeat steps in a process. If you are making a cake the recipe might say add an ingredient and then stir it 50 times or until the batter is smooth. The recipe would not say the word “stir” 50 times. It instead would instruct you to “stir 50 times” or “stir until smooth”.
When our programs need to repeat a step or series of steps, we want to use this same trick to make
the programs easier to write and read.
While writing turtle programs, we have seen many examples of steps repeated. Here is our recipe
for a turtle
alex to draw a hexagon:
1alex.forward(100) 2alex.right(60) 3alex.forward(100) 4alex.right(60) 5alex.forward(100) 6alex.right(60) 7alex.forward(100) 8alex.right(60) 9alex.forward(100) 10alex.right(60) 11alex.forward(100) 12alex.right(60)
There are six repetitions of the
right commands. Although copy/paste can save
us from having to type the lines six times, it is still not ideal. Remember that there is a general
rule in programming: DRY - don’t repeat yourself. Repeating ourselves in this case makes for a
long chunk of code that is awkward to read - you have to stop and think “OK, how many times is this
repeating… ah, six… OK, this must be a hexagon.”
We would like to rewrite our code to tell alex to repeat the
right steps 6
times each instead of listing them six times. We can do so with a
A loop in a program is anything that causes lines of code to get executed multiple times
in a row. A
for loop is a specific kind of loop in Python that uses a list of values and
a series of steps and repeats all of the steps for each item in the list.
In this case, the steps that are repeated are the
right commands that
are indented after the
for. Notice that the
The list of items the
for uses to determine how many times to repeat is created by the
range function. This is a function built into Python that can be used to generate a list of numbers.
This example shows exactly what
For now, we don’t need to worry about exactly what values
range produces. What is
important is that
range(6) makes a list that has 6 values. When that list is
used in a
for statement, it will cause the code to repeat 6 times. Each repetition
is called an iteration and will repeat all of the code indented past the
(To **iterate* is to go through items one at a time.)*
That just leaves the variable side in the for statement to explain:
It is the name we will use for each item as we iterate through the the values
produces. Right now we don’t really need to name those values - we don’t make use of them.
But if we wanted to print out each of the values that range produces, we would use
to refer to “the value for the current iteration”. The program below does exactly that.
In the loop, in addition to doing the turtle commands, it prints out
side to tell
us when it starts each side of the shape.
The body of a loop is the part that gets repeated. It is the lines of code after the
for that are indented past the
for itself. (Like how the body of a procedure is
Notice that a blank line does not “end” the body of the
print("side is currently", side)
alex.right are part of the body and get repeated. The loop
body ends at the first line that is indented to the same level as the
for. In this case,
print("Done with program") is indented to the same level as
for, so at that
point the loop body is done and the line is not part of the loop. That last print will only
happen one time.
The code that is the body of a loop must be indented one step past the loop. The loop body ends at the first line that is indented the same (or fewer) spaces.