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?
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(3) to make 3
sides instead of 4:
Hmmm… that didn’t quite work. This is because of line 7.
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!
120 and then run the program again.
126.96.36.199. 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
left(), we can make ONE loop that
works for ALL polygons.
Make the following adjustments in the editor above:
Before the loop, define a variable called
num_sidesand assign it a value. This stores the number of sides of the shape we want to draw.
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.
turn_angleneeds 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.
Replace the number in
rangewith the variable
Replace the number in
Test the program by having it draw a square first (
num_sides = 4), then change
num_sidesto make a triangle.
Ever wanted to know what a nonagon looks like (9 sides)? How about a triskaidecagon? Change the value of
num_sidesto find out!
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.