STARLEAP / PYTHON TURTLE

Build 2026.09.17-001210-0400

On this page

2. Turtle Loops: Repeat A Chunk

In the last lesson, variables let one value control many lines. Today you will use a for loop to make Python repeat a chunk of code for you.

The pattern starts like this:

CODE
for i in range(4):
    print('again')

The colon and the whitespace at the beginning of the next line both matter.

Step 1 - A Tiny Loop Before Turtle

Save a new file named loop_warmup.py.

Type this program.

CODE
print('start')

for i in range(3):
    print('again')

print('done')

Before you run the program, say what should print first, what should repeat, and what should print last. Then run it and check whether the output matches your prediction.

The Left Edge Matters

The white space at the beginning of a line is Python's ownership map.

A line that starts farther to the right is part of a chunk owned / managed by the closest line above it that is tabbed over less. In this lesson, the less-indented owner line is the for line ending with a colon.

Usually your editor makes four spaces when you press Tab. Four spaces is the usual Python indent.

Step 2 - Same Words, Different Ownership

Program A:

CODE
for i in range(3):
    print('hip')

print('hooray')

Before you run Program A, say what will print once and what will print three times.

Program B:

CODE
for i in range(3):
    print('hip')
    print('hooray')

Before you run Program B, notice that print('hooray') moved to the right. Now say what will change.

Run both versions. The only difference is whether the loop owns the print('hooray') line.

Step 3 - Step Out On Purpose

Your editor will probably keep indenting after a colon. That is helpful, but it is not in charge. You are.

Do this drill once:

Use this exact program for one round of the drill.

CODE
for i in range(4):
    print('inside')

print('outside')

Run the program. inside should print four times. outside should print once.

Step 4 - The Old Turtle Square

Save a new file named loop_square.py.

Start from your turtle template.

CODE
import turtle

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

# Your turtle code goes here.

screen.mainloop()

Replace the middle comment with the old long-way square.

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

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

t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)

screen.mainloop()

Run the program. It works, but notice the repeated chunk: forward, turn, forward, turn.

Step 5 - The Loop Square

Replace the long-way square with this shorter version.

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

for i in range(4):
    t.forward(100)
    t.left(90)

screen.mainloop()

Run the program.

Check that the loop version draws the same square. The two indented lines are the loop's chunk.

Step 6 - A Whitespace Bug Worth Meeting

Change the middle code to this on purpose.

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

for i in range(4):
    t.forward(100)
t.left(90)

screen.mainloop()

Predict before you run it: What repeats? What runs only once?

Run the program.

Fix it by moving t.left(90) back into the loop. It should be indented under the for line.

Step 7 - Change Two Numbers, Get New Shapes

Try a triangle by changing the loop count and the turn.

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

for i in range(3):
    t.forward(100)
    t.left(120)

screen.mainloop()

Try a hexagon.

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

for i in range(6):
    t.forward(70)
    t.left(60)

screen.mainloop()

The triangle uses 3 repeats and a 120-degree turn. The hexagon uses 6 repeats and a 60-degree turn.

Teacher Check

Show a square drawn by a loop. Point at the for line, then point at the loop body.

Say this out loud: "The indented lines belong to the loop. The loop runs that chunk once for each pass in range."