17.5. Using Repetition with Turtles

Learning Objectives:

We already had a turtle draw a square. We repeated the lines in order to make the turtle go forward and turn four times. Another way to do this is to tell the computer to do something explicitly for a certain number of times by using a for loop. The lines that you want to repeat in the for loop must be indented by 4 spaces as shown below.

Run the code to see what it draws.

The following program uses a turtle to draw a rectangle as shown below, but the lines are mixed up. The program should do all necessary set-up and create the turtle. After that, iterate (loop) 2 times, and each time through the loop the turtle should go forward 175 pixels, turn right 90 degrees, go forward 150 pixels, and turn right 90 degrees. Drag the needed blocks of statements from the left column to the right column and put them in the right order with the correct indention. There may be additional blocks that are not needed in a correct solution. Click on Check to see if you are right. You will be told if any of the lines are in the wrong order or are the wrong blocks.


Since it doesn’t matter what’s in the list, just as long as there are four items, there is a special way of writing that loop. We use a range function.

Run the code to see what it draws.

The range(n) function returns an object (a range object) that produces the value from 0 to n - 1 when you use it in a for-each loop as shown below.

Run this code to see what it prints.

You have attempted of activities on this page