Drawing a Stop Sign¶
By the end of this lesson you'll be able to:
- Use variables to store the color, size, and number of sides of a shape
- Use a
forloop to draw any regular polygon without repeating code - Fill a shape with color using
begin_fill()andend_fill()
A stop sign is an octagon — a regular 8-sided polygon. Instead of writing eight
forward/right pairs by hand, you'll use a loop and a formula to draw it in just
a few lines.
Welcome to This Lesson!
Ready to level up? In this lesson we're going to draw a real stop sign —
red fill, white STOP text, and everything. A for loop is going to do all the
heavy lifting for us. Let's code it together!
Using Variables for Shape Properties¶
When you store the size and color in variables, changing the whole drawing is as easy as editing one line. A variable is a named box that holds a value — give it a descriptive name so the code is easy to read.
1 2 3 | |
The turn angle for any regular polygon follows a simple formula: divide 360 by the number of sides. For an octagon: 360 ÷ 8 = 45 degrees.
Scratch Bridge
In Scratch you used variables in the Variables drawer — set [distance] to 50.
In Python, distance = 50 does exactly the same thing: it creates a variable named
distance and stores the value 50 in it.
Filling a Shape with Color¶
Two commands work as a pair to paint the inside of any shape:
begin_fill()— tells the turtle "start remembering the path"end_fill()— tells the turtle "now fill in everything inside that path"
Everything drawn between those two calls gets filled with the current color. If you forget either one, the fill won't appear.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
What Do You Think Will Happen?
Look at the line for i in range(sides): — and remember that sides = 8.
How many times will the loop repeat? And what color will the inside of the shape be?
Make your guesses — then click Run!
Try It Now¶
Edit the code below and click Run to draw the stop sign right on this page. No account needed — everything runs in your browser.
Were you right? The loop runs 8 times (once for each side of the octagon) and the
inside fills with red because my_color = "red" was set before begin_fill().
How It Works¶
The turn angle formula 360 / sides is the secret: the exterior angles of any regular
polygon always add up to exactly 360 degrees. Divide by the number of sides and you get
the angle for one corner.
goto(-50, 100) moves the turtle to a starting corner without drawing — penup() lifts
the pen first so no line appears. pendown() lowers it again before drawing begins.
hideturtle() removes the turtle icon when drawing is finished so it doesn't cover the artwork.
write('STOP', font=...) places text at the turtle's current position using the
specified font family, size, and style.
Explanation Table¶
| Line | What it does |
|---|---|
distance = 50 |
Length of each side in pixels |
sides = 8 |
Number of sides — change this to get a different polygon |
angle = 360 / sides |
Turn angle — the formula works for any regular polygon |
my_color = "red" |
Stores the color in a variable so it's easy to change |
penup() / goto() / pendown() |
Move to start position without drawing a line |
begin_fill() / end_fill() |
Fill everything drawn between them with the current color |
for i in range(sides): |
Repeat the loop body once for each side |
hideturtle() |
Hides the turtle cursor when drawing is done |
write('STOP', font=...) |
Writes text at the turtle's current position |
Learning Check¶
Spot the Bug!
The program below should draw a filled red octagon, but when you run it the shape
has no fill — just an outline. One line is in the wrong place. Can you find it and
fix it?
Experiments¶
Try these changes to the stop sign code above. Predict what will happen first, then run it!
-
Change
my_color = "red"to"blue". You'll know it worked when the octagon fills with blue instead of red. -
Change
distance = 50to80. You'll know it worked when the stop sign is noticeably larger. -
Change
sides = 8to6— and also updateangle = 360 / sidesstays the same (it recalculates automatically). You'll know it worked when you see a filled hexagon instead of an octagon. -
Change
'STOP'to your own word. You'll know it worked when your word appears on the sign in white. -
Change
monty.color('white')(the text color) to'yellow'. You'll know it worked when the text on the sign turns yellow. Can you still read it easily?
Great Work!
You drew a stop sign using variables, a loop, and fill commands — that's serious Python!
Try changing sides to 3 or 5 and see what signs you can invent.
Let's code it together!