6.4. Iteration Improves the Turtle Program

Take another look at the turtle code for drawing a square. Which statements are repeated? How many times?

Example

Turtle statements for drawing a square:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import turtle

pet = turtle.Turtle()

pet.forward(50)
pet.left(90)
pet.forward(50)
pet.left(90)
pet.forward(50)
pet.left(90)
pet.forward(50)
pet.left(90)

We can DRY the code by using a for loop. Complete the code below by entering a value into range. Next, copy and paste the SMALLEST number of statements into the loop body that will make the turtle draw a square. Don’t forget to indent!

Nice! By adding a loop, you were able to use only 3 lines of code to draw a square instead of the original 8.

While “saving some lines of code” might be convenient, it is NOT the big deal here. What is much more important is that we found a repeating pattern of statements, and we changed our program to repeat the pattern.

Finding repeatable chunks of code and building our programs around those chunks is a vital skill for coding.

6.4.1. How About Drawing Other Shapes?

What if we want the turtle to draw a triangle instead of a square? Note that range(4) made the loop run 4 times and produced 4 lines. It seems like we can change the value in range to make other shapes.

For a triangle, let’s try replacing range(4) with range(3) to make 3 sides instead of 4:

Example

Hmmm… that didn’t quite work. This is because of line 7. bob.left(90) works fine for the square (all right angles), but the turtle needs to turn by a different amount for a triangle.

Without getting deep into the geometry here, the answer is… 120 degrees! Replace 90 with 120 and then run the program again.

6.4.1.1. Now Add Variables!

We built one for loop to draw a square and a separate for loop to draw a triangle. However, both loops do essentially the same thing—move the turtle and then turn the turtle.

Rather than coding separate loops for every possible polygon (triangle, square, pentagon, hexagon, etc.) we should DRY the loop! By using variables instead of specific numbers inside range and left(), we can make ONE loop that works for ALL polygons.

Make the following adjustments in the editor above:

  1. Before the loop, define a variable called num_sides and assign it a value. This stores the number of sides of the shape we want to draw.
  2. Also before the loop, define a variable called turn_angle. This stores the number of degrees that the turtle needs to rotate after drawing each side.
  3. turn_angle needs to be calculated. Set the variable equal to 360.0 / num_sides (360.0 degrees divided by the number of sides). If you want to know why this works, go ask your math teacher! They will LOVE to explain it to you.
  4. Replace the number in range with the variable num_sides.
  5. Replace the number in left with turn_angle.
  6. Test the program by having it draw a square first (num_sides = 4), then change num_sides to make a triangle.
  7. Ever wanted to know what a nonagon looks like (9 sides)? How about a triskaidecagon? Change the value of num_sides to find out!

Tip

If you have your own repl.it account, you can save a copy of the finished polygon loop by logging in and forking the final code.