STARLEAP / PYTHON TURTLE
Build 2026.09.17-001210-0400
On this page
7. Random Flower Field: Let Python Choose
You have placed flowers by choosing the exact position, color, and size yourself. Today you will let Python choose some of those values.
Random values make a program feel different each time it runs. Python's random module can choose numbers and items from a list.
Start With A Turtle File
Save a new file named random_flower_field.py.
Start with this template.
import random
import turtle
screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()
t.speed(0)
t.pensize(2)
# Your random flower code goes here.
screen.mainloop()
Run the program. A blank turtle window is fine. Close the turtle window before you change the code again.
Step 1 - Choose One Random Number
Replace the middle comment with this code.
t.speed(0)
t.pensize(2)
x_position = random.randint(-250, 250)
print(x_position)
screen.mainloop()
Run the program a few times.
random.randint(-250, 250) chooses a whole number from -250 through 250. The printed number may be different each time you run the program.
Step 2 - Move To A Random Spot
Add a random y_position.
x_position = random.randint(-250, 250)
y_position = random.randint(-300, 250)
print(x_position, y_position)
screen.mainloop()
Then move the turtle to that spot.
print(x_position, y_position)
t.teleport(x_position, y_position)
t.dot(12)
screen.mainloop()
Run the program a few times. The dot should appear in a new place.
t.teleport(x_position, y_position) jumps to the random spot without drawing a travel line.
Step 3 - Choose A Random Color
Make a list of colors.
y_position = random.randint(-300, 250)
colors = ['purple', 'red', 'orange', 'blue', 'teal']
ink = random.choice(colors)
print(ink)
t.teleport(x_position, y_position)
...
Use the chosen color before drawing the dot.
ink = random.choice(colors)
t.color(ink)
t.teleport(x_position, y_position)
t.dot(12)
Run the program a few times.
Step 4 - Bring Back The Flower Function
Replace the dot with a small flower function.
colors = ['purple', 'red', 'orange', 'blue', 'teal']
ink = random.choice(colors)
def draw_flower(radius, arc_angle):
for i in range(8):
t.circle(radius, arc_angle)
t.left(180 - arc_angle)
t.circle(radius, arc_angle)
t.left(180)
t.color(ink)
t.teleport(x_position, y_position)
...
Then call the flower at the bottom.
t.teleport(x_position, y_position)
draw_flower(25, 60)
screen.mainloop()
Run the program. Each run should place one flower somewhere new.
Step 5 - Repeat The Random Flower
Put the random choices inside a loop.
def draw_flower(radius, arc_angle):
...
for i in range(10):
x_position = random.randint(-250, 250)
y_position = random.randint(-300, 250)
ink = random.choice(colors)
t.color(ink)
t.teleport(x_position, y_position)
draw_flower(25, 60)
screen.mainloop()
Run the program.
The loop chooses a new position and color before each flower. That is why the field fills with different flowers.
Step 6 - Random Sizes Too
Choose a radius inside the loop.
for i in range(10):
x_position = random.randint(-250, 250)
y_position = random.randint(-300, 250)
ink = random.choice(colors)
radius = random.randint(12, 35)
t.color(ink)
...
draw_flower(25, 60)
Use that radius in the call.
for i in range(10):
...
t.teleport(x_position, y_position)
draw_flower(radius, 60)
Run the program.
Final Project - Random Flower Field
Use this full program to grow a random field.
import random
import turtle
screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()
t.speed(0)
t.pensize(2)
colors = ['purple', 'red', 'orange', 'blue', 'teal']
def draw_flower(radius, arc_angle):
for i in range(8):
t.circle(radius, arc_angle)
t.left(180 - arc_angle)
t.circle(radius, arc_angle)
t.left(180)
for i in range(18):
x_position = random.randint(-250, 250)
y_position = random.randint(-300, 250)
ink = random.choice(colors)
radius = random.randint(12, 35)
t.color(ink)
t.teleport(x_position, y_position)
draw_flower(radius, 60)
screen.mainloop()
Run the program more than once. Notice what stays the same and what changes.
Make the field yours by changing one value at a time. Good experiments include the number in range(18), the position ranges, the color list, and the radius range.
Teacher Check
Show your random flower field. Then point to:
- one
random.randint(...)line - one
random.choice(...)line - the loop that repeats the flower
- the function call that uses a random radius
Say this out loud: "The loop repeats the same plan, but random values make each flower different."