STARLEAP / PYTHON TURTLE

Build 2026.09.17-001210-0400

On this page

1. Turtle Variables: One Change Controls Many Lines

You already know how to start a turtle program and give the turtle orders. Today the new move is a variable: a name that stands for a value. A variable is like a sticky note. Write the name on the note, stick it to a value, and then use the name wherever you need that value.

Surprise: you have already been using variables. screen is the name for the turtle window. t is the name for the turtle that moves. Today you will make more variables on purpose.

Today you will use variables to control distance, shape size, color, and line thickness.

Start With Your Turtle Template

Save a new file named turtle_variables.py.

Type this whole program once.

CODE
import turtle

screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()

# Your turtle 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 - Add A Distance Variable

Find the middle of your program: after t = turtle.Turtle() and before screen.mainloop(). Replace the middle comment with the black lines.

In this code box, gray lines are already in your program. Use them to find the right spot. Add or change the black lines.

EDIT VIEW - Gray = already there; dark = add or change
import turtle

screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()

length = 80

t.forward(length)
t.left(90)
t.forward(length)

screen.mainloop()

Run the program.

Notice that the turtle used length both times it moved forward. Both forward lines fetched the same value.

Step 2 - One Edit, Two Effects

Change only the value of length.

EDIT VIEW - Gray = already there; dark = add or change
t = turtle.Turtle()

length = 140

t.forward(length)
t.left(90)
t.forward(length)

screen.mainloop()

Run the program again.

Watch both sides change even though you only edited the value once. That is why variables are useful: one change can control many lines.

Step 3 - Two Variables Make A Rectangle

Replace the middle turtle code with this version.

EDIT VIEW - Gray = already there; dark = add or change
t = turtle.Turtle()

width = 150
height = 70

t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.forward(width)
t.left(90)
t.forward(height)

screen.mainloop()

Run the program.

Notice which sides use width and which sides use height.

Now look for another repeated value. The turn angle 90 appears more than once. That number could become a variable too.

Change only the two variable values.

EDIT VIEW - Gray = already there; dark = add or change
t = turtle.Turtle()

width = 90
height = 140

t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.forward(width)
t.left(90)
t.forward(height)

Run the program again. Watch the rectangle change from wide to tall without changing the turtle command lines.

Step 4 - Why Variables Are Worth It

Now draw a square the long way. Replace the middle code with this.

EDIT VIEW - Gray = already there; dark = add or change
t = turtle.Turtle()

side = 90
angle = 90

t.forward(side)
t.left(angle)
t.forward(side)
t.left(angle)
t.forward(side)
t.left(angle)
t.forward(side)

screen.mainloop()

Run the program.

Try side = 50, then side = 130. Run the program after each change.

This square still has a lot of repeated lines. Notice the repeated chunk: forward, turn, forward, turn. Next class, Python will repeat that chunk for you.

Also notice that angle now controls every turn. For a square, the turn angle is 90 degrees. Later, different angle values will help make different shapes.

Step 5 - Variables Can Hold Style Too

Numbers are not the only values. Text values go in quotes.

Replace the middle code with this.

EDIT VIEW - Gray = already there; dark = add or change
t = turtle.Turtle()

side = 100
angle = 90
ink = 'purple'
thickness = 5

t.color(ink)
t.pensize(thickness)

t.forward(side)
t.left(angle)
t.forward(side)
t.left(angle)
t.forward(side)
t.left(angle)
t.forward(side)

screen.mainloop()

Run the program.

Check each variable by changing only one value, then running the program. side controls the size, ink controls the color, and thickness controls the line thickness.

Important: Use quotes when you write the text value, like 'purple'. Do not put quotes around the variable name when you use it later.

Step 6 - Make It Yours

Choose your own values on purpose. These are examples; yours can be different.

EDIT VIEW - Gray = already there; dark = add or change
t = turtle.Turtle()

side = 120
angle = 90
ink = 'green'
thickness = 8

t.color(ink)
t.pensize(thickness)

Run the program after each edit, not after all three. Programmers test one small change at a time.

Teacher Check

Show your finished square. Then point to the line that controls each of these:

Say this out loud: "A variable is a name that stands for a value. When I change the value once, every line that uses the variable uses the new value."