4.5. Iteration Simplifies our Turtle Program

To draw a square we’d like to do the same thing four times — move the turtle forward some distance and turn 90 degrees. We previously used 8 lines of Python code to have alex draw the four sides of a square. This next program does exactly the same thing but, with the help of the for statement, uses just three lines (not including the setup code). Remember that the for statement will repeat the forward and left four times, one time for each value in the list.

While “saving some lines of code” might be convenient, it is not the big deal here. What is much more important is that we’ve found a “repeating pattern” of statements, and we reorganized our program to repeat the pattern. Finding the chunks and somehow getting our programs arranged around those chunks is a vital skill when learning How to think like a computer scientist.

The for-loop is our first example of a compound statement. Syntactically a compound statement is a statement. The level of indentation of a (whole) compound statement is the indentation of its heading. In the example above there are two statements with the same indentation, executed sequentially: the import and the whole for-loop. The for-loop compound statement is executed completely before ending the program.

Mixed up program

        turtle-6-1: The following program uses a turtle to draw a triangle as shown to the left,  but the lines are mixed up.  The program should do all necessary set-up and create the turtle.  After that, iterate (loop) 3 times, and each time through the loop the turtle should go forward 175 pixels, and then turn left 120 degrees.  After the loop, set the window to close when the user clicks in it.

Drag the blocks of statements from the left column to the right column and put them in the right order with the correct indention. Click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order or are incorrectly indented.

import turtle --- # repeat 3 times for i in range(0, 3, 1): --- turtle.forward(175.0) --- turtle.left(120.0)

Mixed up program

        turtle-6-2: The following program uses a turtle to draw a rectangle as shown to the left,  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.  After the loop, set the window to close when the user clicks in it.

Drag the blocks of statements from the left column to the right column and put them in the right order with the correct indention. Click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order or are incorrectly indented.

import turtle --- # repeat 2 times for i in range(0, 2, 1): --- turtle.forward(175.0) --- turtle.right(90.0) --- turtle.forward(150.0) turtle.right(90.0)

Check your understanding

    turtle-6-3: In the following code, how many lines does this code print?

    for i in range(0, 6, 1):
        print("Thank you.  May I have another?")
    
  • 1
  • The loop body prints one line, but the body will execute multiple times.
  • 5
  • The loop body will execute 6 times.
  • 6
  • The loop body will execute 6 times.
  • 10
  • The loop body will execute 6 times.

    turtle-6-4: How does python know what statements are contained in the loop body?

  • They are indented to the same degree from the loop header.
  • The loop body can have any number of lines, all indented from the loop header.
  • There is always exactly one line in the loop body.
  • The loop body may have more than one line.
  • The loop body ends with a semi-colon (;) which is not shown in the code above.
  • Python does not need semi-colons in its syntax, but relies mainly on indentation.

    turtle-6-5: Consider the following code:

    for i in range(0, 4, 1):
       turtle.forward(50.0)
       turtle.left(90.0)
    

    What does each iteration through the loop do?

  • Draw a square.
  • The question is not asking you to describe the outcome of the entire loop, the question is asking you about the outcome of a **single iteration** of the loop.
  • Draw two sides of a square.
  • Notice that the turtle only moves forward once in the loop body so it can't draw two sides.
  • Draw one side of a square.
  • The body of the loop only draws one side of the square. It will be repeated once for each item in the list.
You have attempted of activities on this page
Next Section - 4.6. The range Function