Community resourceWorksheet
TUR-02 Shape Machine — named Turtle
Part 2 of 6 · Python Turtle (named Turtle)
Use repetition to turn a sequence of named-Turtle instructions into regular shapes.
Students will:
- identify the repeated steps in a shape
- use counting loops to reduce repeated code
- control a shape with variables
Inside: worked examples, objective checks and practical Turtle challenges.
Series: Python Turtle (named Turtle), part 2 of 6.
Shared by Coding PathwayVerified teacher
- 21 cells
- About 45 minutes
- CC BY-SA 4.0
- Shared 16 Sept 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Shape Machine
In this lesson, you will learn how to use for loops to draw shapes with much less code. By the end, changing one variable will let your program draw a different regular shape.
Before starting with for loops, complete the external-angle calculation from Turtle Steps.
turn_angle = gap 1 / gap 2- 360
- 180
- sides
- side_length
What is a for loop?
For loops repeat lines of code for us. This saves time and effort when we write longer programs. With Turtle, when we know how many times instructions need to repeat, we can use a for loop.
The colon : starts the for loop. The indented lines underneath are the for loop body. These are the instructions that repeat.
In this example, range(4) means repeat four times:
for count in range(4):
artist.forward(side_length)
artist.right(turn_angle)
count is the for loop's counter. Python changes it after each repeat. This drawing does not need to use its value.
Run the example and watch it complete all four sides.
import turtle
artist = turtle.Turtle()
artist.speed(4)
side_length = 80
turn_angle = 360 / 4
for count in range(4):
artist.forward(side_length)
artist.right(turn_angle)Which instructions repeat in the example?
- AOnly the forward instruction
- BThe two indented instructions
- CEvery line in the program
- DOnly the turn instruction
Use sides in two places
The variable sides holds the number of sides in the shape. We can use this value in two places:
turn_angle = 360 / sides
for count in range(sides):
# drawing instructions go here
First, 360 / sides calculates the external turn angle. Second, range(sides) tells the for loop how many times to repeat. Changing sides updates both parts of the program.
Make sides control the for loop
In the code below, replace the 4 so the for loop uses the value inside sides. Then change the value held by sides and run the code again. Try 3, 5 and 8. Notice how easily the for loop draws different shapes.
Need the for loop line?
for count in range(sides):
# the repeated instructions stay indented
import turtle
artist = turtle.Turtle()
artist.speed(0)
sides = 5
side_length = 70
turn_angle = 360 / sides
for count in range(4):
artist.forward(side_length)
artist.right(turn_angle)If sides is 6 and the for loop uses range(sides), how many times does its body run?
- A5
- B60
- C360
- D6
Reading a for loop error
If a for loop will not run, check the colon and indentation first. A missing colon causes a syntax error. Missing indentation means Python cannot tell which instructions belong to the for loop.
Repair the for loop line and indentation.
for count in range(sides)gap 1
gap 2artist.forward(side_length)
artist.right(turn_angle)- :
- four spaces
- ;
- no spaces
Draw a triangle
The code below currently stores 6 in sides. Change this to the number of sides in a triangle. Then add side_length, calculate turn_angle and write the complete for loop.
Need a reminder?
Use turn_angle = 360 / sides. Inside the for loop, move forward and then turn. Both instructions need the same indentation.
import turtle
artist = turtle.Turtle()
artist.speed(0)
sides = 6Draw a hexagon
Complete the value after sides =. Then add a side_length variable, calculate the external turn angle and write a complete for loop to draw a hexagon.
Try to build this program from the steps above. If you get stuck, use the earlier example to check the order.
import turtle
artist = turtle.Turtle()
sides =Draw an octagon
Complete the value after sides =, then build a program that draws a regular octagon. You need to create the other variables, calculate the turn and write the for loop yourself.
Check before you run
Have you added side_length? Does turn_angle use 360 / sides? Does range(sides) control the for loop? Are both drawing instructions indented?
import turtle
artist = turtle.Turtle()
sides =Core check
You are ready when you can:
- create the variables a shape needs;
- calculate its external turn angle;
- write a complete
forloop; - make the triangle, hexagon and octagon close.
Optional pattern challenge
The code below uses one for loop inside another. The inner for loop draws one square. The outer for loop repeats the whole square and turns before drawing the next one.
Run it once. Then change number_of_shapes, side_length or the outer turn calculation to design a different pattern.
How do the two for loops work?
Python completes the inner for loop before it moves to the outer for loop's final turn. You do not need nested for loops for the core lesson; this is an extension.
import turtle
artist = turtle.Turtle()
artist.speed(0)
sides = 4
side_length = 60
turn_angle = 360 / sides
number_of_shapes = 6
for shape_count in range(number_of_shapes):
for side_count in range(sides):
artist.forward(side_length)
artist.right(turn_angle)
artist.right(360 / number_of_shapes)What does range(sides) control in your for loop?
Answer in one sentence.
Students type their answer here.