Random Stars¶
By the end of this lesson you'll be able to:
- Write a function that draws a five-pointed star using
forward()andright(144) - Call that function with random positions, sizes, and colors from a list
- Combine functions, lists, and randomness in one program
Five-pointed stars have a secret: the turtle always turns 144 degrees and draws 5 spokes. In this lab you'll write a star() function, scatter it at random positions, and let Python pick the colors — so every run produces a new piece of star art.
Welcome to This Lesson!
Stars, colors, and randomness — all in one program! This lab puts together everything you've
learned so far: functions, lists, loops, and random. Let's code it together!
Drawing a Star¶
A five-pointed star uses a special angle: the turtle draws a spoke, turns 144 degrees right, draws another spoke — and repeating this 5 times closes the star perfectly. The spoke length controls the star's size.
1 2 3 4 5 6 7 8 9 | |
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?
The loop runs 8 times and each call picks a random position, size, and color.
How many stars will appear? Will they be the same size? Click Run twice — do you get the same picture?
Make your guess — then 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? Eight filled stars — each a different random color and size, scattered across the canvas. Click Reset then Run again and you'll get a brand-new arrangement every time!
How It Works¶
The key to the star shape is right(144). Because 144 × 5 = 720 = 2 × 360, the turtle makes exactly two full rotations while tracing the five spokes, ending up where it started. Any other angle and the shape won't close into a star.
len(colors) - 1 computes the last valid index of the list dynamically, so adding or removing colors from the list automatically adjusts the random range — no need to update the number by hand.
Explanation Table¶
| Line | What it does |
|---|---|
for i in range(5): forward(size); right(144) |
Draws the 5 spokes of the star |
right(144) |
The magic star angle — 5 × 144 = 720° = two full rotations |
random.randint(0, len(colors) - 1) |
Picks a random valid index into the colors list |
size = random.randint(20, 50) |
Picks a random star size between 20 and 50 steps |
Stars That Don't Close
If you change right(144) to any other angle, the 5-spoke loop won't bring the turtle
back to its start — and the star won't close. Try right(150) and see what happens!
Learning Check¶
Your Turn — Add a Background Color
The program below draws random stars but the background is plain white.
Add one line right after creating monty to set the screen background color to 'navy'.
Hint: use turtle.Screen().bgcolor('navy').
Experiments¶
Try these changes to the random-stars code. Predict first, then run!
-
Change
range(8)torange(20). You'll know it worked when 20 stars crowd the canvas. -
Change
random.randint(20, 50)forsizetorandom.randint(10, 80). You'll know it worked when stars range from tiny to very large. -
Change
range(5)torange(6)andright(144)toright(120)inside the function. You'll know it worked when you see 6-pointed Stars of David instead of 5-pointed stars. -
Add
monty.pensize(2)inside thestarfunction to give each star a visible outline. You'll know it worked when each star has a thin border. -
Remove
begin_fill()andend_fill()from the function. You'll know it worked when you see outline-only stars instead of filled ones.
Great Work!
You combined functions, lists, loops, and randomness into one creative program —
that's real Python programming! In the next lab you'll learn how to get input from the user.
Let's code it together!