STARLEAP / PYTHON TURTLE
Build 2026.09.17-001210-0400
On this page
3. Regular Polygon Builder: Variables Plus Loops
A regular polygon is a closed shape where every side has the same length and every turn has the same angle. Squares, equilateral triangles, pentagons, hexagons, and octagons are all regular polygons.
Today you will combine variables and a loop so one program can draw a regular polygon with any given number of sides. For now, "given" means you set the number in a variable at the top of the program.
Step 1 - A Square With Named Values
Save a new file named polygon_builder.py.
Start from your turtle template and put the black lines in the middle.
import turtle
screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()
sides = 4
length = 100
angle = 90
for i in range(sides):
t.forward(length)
t.left(angle)
screen.mainloop()
Run the program.
Notice the three jobs. sides tells the loop how many times to repeat, length controls each side, and angle controls the turn after each side.
Step 2 - Find The Turn Angle
Keep the same program shape. First change sides and length. Then put your first test number on the angle = ____ line, run the program, and notice whether the shape closes correctly.
Triangle:
t = turtle.Turtle()
sides = 3
length = 110
angle = ____
for i in range(sides):
...
Now change only angle and run again. Keep trying until the triangle closes.
Hexagon:
t = turtle.Turtle()
sides = 6
length = 70
angle = ____
for i in range(sides):
...
Now change only angle and run again. Keep trying until the hexagon closes.
Hint: if the turtle does not turn enough, the path will stay too open. If the turtle turns too much, the path will fold inward. The turtle closes the shape when all the turns add up to 360 degrees.
Step 3 - Let Python Calculate The Turn
If a closed polygon needs a total turn of 360 degrees, then each turn is:
angle = 360 / sides
print(angle)
Replace the top variables with this.
t = turtle.Turtle()
sides = 5
length = 85
angle = 360 / sides
print(angle)
for i in range(sides):
t.forward(length)
t.left(angle)
Run the program.
The printed value is proof that Python calculated the turn angle. Five sides makes a pentagon. The angle is 72 degrees because 360 / 5 is 72.
Step 4 - Any Given Number Of Sides
Now your program can draw many polygons by changing one main variable.
Try these values. Choose a smaller length when the number of sides gets bigger.
- 7 sides, length 75
- 8 sides, length 65
- 10 sides, length 55
- your own number of sides, with a length that keeps the drawing on screen
Step 5 - Final Program
Here is the whole program in one place. Type it carefully, then make it yours by changing only the variables near the top.
import turtle
screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()
t.speed(0)
sides = 7
length = 70
angle = 360 / sides
print(angle)
ink = 'teal'
thickness = 4
t.color(ink)
t.pensize(thickness)
for i in range(sides):
t.forward(length)
t.left(angle)
screen.mainloop()
Run the program after each small change.
Change only sides and run the program. Then change only length and run it again. Then change only ink or thickness. One small change per run keeps the program easy to understand.
Step 6 - Make A Polygon You Can Explain
Pick the number of sides, side length, color, and thickness on purpose. Make sure the whole shape fits on the screen.
Teacher Check
Show your final polygon. Then point to:
- the
sidesvariable - the
angle = 360 / sidesline - the
for i in range(sides):line - the two indented lines owned by the loop
Say this out loud: "The loop draws one side and one turn each pass. The variable sides controls how many passes happen. The angle formula makes all the turns add up to 360 degrees."