Skip to content

Drawing Polygons with Loops

By the end of this lesson you'll be able to:

  • Use for i in range(n): to repeat a block of code n times
  • Draw any regular polygon by changing just two variables
  • Explain why a loop is better than writing the same lines over and over

Drawing a square in the last lesson required eight lines — four forward() calls and four right() calls. A for loop does that in two lines. Better yet, change one variable and you instantly get a pentagon, hexagon, or any polygon you like.

Welcome to This Lesson!

Monty waving welcome Here's a secret every programmer knows: if you're copying and pasting the same line, there's a better way. Today I'll show you the for loop — it's Monty's favorite shortcut. Let's code it together!

The For Loop

A for loop is a block of code that runs a set number of times. Here's the pattern:

1
2
3
for i in range(4):
    monty.forward(80)
    monty.right(90)

range(4) counts from 0 to 3, so the indented block runs 4 times. The variable i tracks which repetition you're on — but for drawing polygons, you don't even need to use i.

The turn angle for any regular polygon follows one rule: divide 360 by the number of sides. An octagon has 8 sides: 360 ÷ 8 = 45 degrees. A hexagon: 360 ÷ 6 = 60 degrees.

Scratch Bridge

Monty with a tip In Scratch you used the "Repeat 4" block to run commands a set number of times. In Python, for i in range(4): does exactly the same thing — and the indented lines beneath it are the commands that repeat!

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import turtle
monty = turtle.Turtle()
monty.shape('turtle')

sides = 6
angle = 360 / sides

for i in range(sides):
    monty.forward(80)
    monty.right(angle)

What Do You Think Will Happen?

Monty thinking sides = 6 and angle = 360 / 6. How many sides will the shape have, and what shape is that? Make your guess — then click Run to find out!

Try It Now

Edit the code below and click Run to see the result right on this page. No account needed — everything runs in your browser.


  

Were you right? Six sides, each at 60 degrees — a perfect hexagon!

If you see a red error message: Check that the two lines inside the loop are indented by exactly 4 spaces. Python uses indentation to know which lines belong inside the loop.

How It Works

for i in range(sides): tells Python to count from 0 up to (but not including) sides. Each count is one repetition of the indented block. The angle formula 360 / sides always gives the correct exterior turn for a regular polygon — it's a mathematical fact that the exterior angles of any convex polygon add up to exactly 360 degrees.

Explanation Table

Line What it does
sides = 6 Number of sides — change this to get a different polygon
angle = 360 / sides Calculates the correct turn angle for this polygon
for i in range(sides): Repeats the indented block once for each side
monty.forward(80) Draws one side (80 steps long); the 4-space indent makes it part of the loop
monty.right(angle) Turns the correct amount at each corner

Common Mistake: Forgetting the Colon

Monty warning The for line must end with a colon : and the lines inside must be indented. If you forget either one, Python will show a SyntaxError. Check both before you search further!

Learning Check

Your Turn — Add the Missing Line

Monty thinking The program below is trying to draw a pentagon (5 sides), but angle is never calculated — so Python doesn't know how far to turn. Add one line to calculate the angle!


  

Experiments

Try these changes to the hexagon code. Predict first, then run!

  1. Change sides = 6 to sides = 3. You'll know it worked when you see a triangle.

  2. Change sides = 6 to sides = 8. You'll know it worked when you see an octagon (8 equal sides).

  3. Change sides = 6 to sides = 36. You'll know it worked when the polygon looks almost like a circle.

  4. Change monty.forward(80) to monty.forward(120). You'll know it worked when the hexagon is larger and may extend to the edge of the canvas.

  5. Add monty.color('purple') and monty.pensize(4) before the loop. You'll know it worked when a thick purple polygon appears.

Great Work!

Monty celebrating One for loop, infinite polygons — you've unlocked a superpower! In the next lab you'll use if/else inside a loop to make each side a different color. Let's code it together!