Section 20.2 Using Repetition with Turtles
While learning how to draw with the turtle, we have found many reasons to repeat ourselves in ways that a loop can make easier. A simple example is a square
procedure - it needs to do both forward
and right
4 times. So why not have a for
loop repeat those for us:
Checkpoint 20.2.1.
The following program should define a procedure triangle that uses a loop to help draw a triangle. The main part of the program calls that function twice to draw two different triangles.
Arrange and indent the blocks correctly. You will not need them all.
def triangle(turtleName):
---
for side in range(3):
---
turtleName.forward(120)
turtleName.right(90)
---
nick.forward(120)
nick.right(90) #distractor
---
#Start of main part of program
from turtle import *
---
nick = Turtle()
triangle(nick)
nick.right(180)
triangle(nick)
Checkpoint 20.2.2.
The following program uses a turtle to draw a 175x150 rectangle as shown below, but the lines are mixed up. We want to define a rectangle
procedure that twice repeats the process to draw half of a rectangle: draw a line for the width, then turn, draw a line for the height, and turn again.
Arrange the blocks into the correct solution and indent them properly. You will not use all of the blocks.
def rectangle(turtle, width, height):
---
# repeat 2 times
for i in range(2):
---
# repeat 2 times
for i in range(2) #paired
---
turtle.forward(width)
turtle.right(90)
---
carlos.forward(width)
carlos.right(90) #paired
---
turtle.forward(height)
turtle.right(90)
---
turtle.forward(150)
turtle.right(90) #paired
---
from turtle import *
---
carlos = Turtle()
rectangle(carlos, 175, 150)
You have attempted
of
activities on this page.