Skip to content

Random Stars

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

  • Write a function that draws a five-pointed star using forward() and right(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!

Monty waving welcome 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
def star(x, y, size):
    monty.penup()
    monty.goto(x, y)
    monty.pendown()
    monty.begin_fill()
    for i in range(5):
        monty.forward(size)
        monty.right(144)
    monty.end_fill()

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
import turtle
import random

monty = turtle.Turtle()
monty.shape('turtle')
monty.speed(10)

colors = ['red', 'orange', 'green', 'blue', 'purple', 'pink', 'gold']

def star(x, y, size):
    monty.penup()
    monty.goto(x, y)
    monty.color(colors[random.randint(0, len(colors) - 1)])
    monty.pendown()
    monty.begin_fill()
    for i in range(5):
        monty.forward(size)
        monty.right(144)
    monty.end_fill()

for i in range(8):
    x = random.randint(-150, 150)
    y = random.randint(-150, 150)
    size = random.randint(20, 50)
    star(x, y, size)

monty.hideturtle()

What Do You Think Will Happen?

Monty thinking 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

Monty warning 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

Monty thinking 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!

  1. Change range(8) to range(20). You'll know it worked when 20 stars crowd the canvas.

  2. Change random.randint(20, 50) for size to random.randint(10, 80). You'll know it worked when stars range from tiny to very large.

  3. Change range(5) to range(6) and right(144) to right(120) inside the function. You'll know it worked when you see 6-pointed Stars of David instead of 5-pointed stars.

  4. Add monty.pensize(2) inside the star function to give each star a visible outline. You'll know it worked when each star has a thin border.

  5. Remove begin_fill() and end_fill() from the function. You'll know it worked when you see outline-only stars instead of filled ones.

Great Work!

Monty celebrating 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!