← Python Foundations with Turtle

Use the counter

The loop counter i changes every pass, so a line can grow without you typing each number.

Progress bars, animation frames and game levels all count. The loop counter is how code counts.

You have met i in every loop so far, but never used it. It is a counter, and it changes on every pass.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

counter.py
from turtle import *

for i in range(30):
    forward(i * 5)
    right(90)

forward(i * 5) uses the counter to work out how far to go. Read the code. What will the turtle draw?

What will the turtle draw?

RunRun it and compare the result with your prediction.

Run

Run it and compare it with your prediction.

Here is what each line does.

Line What it does Why
for i in range(30): Runs 30 times and makes a counter called i i is 0 on the first pass, 1 on the second, up to 29
forward(i * 5) Draws a line i * 5 long The lines are 0, 5, 10, 15 and so on, so they grow
right(90) Turns a quarter turn The same turn every pass

The first line is 0 steps long, because i is 0 on the first pass. Counting starts at 0, not 1.

counter.py
from turtle import *

for i in range(30):
    forward(i * 5)
    right(90)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

Follow the counter round the loop, then use what you know about names.

counter.py
from turtle import *

for i in range(30):
    forward(i * 5)
    right(90)

Work out i and the length of the line on each pass.

A name holds a value you can calculate with.

Fill in the counter and the length of the line on each pass.

passiline length (i * 5)
100
2
3
4

A program has sides = 8 and turns right(360 / sides) on every pass of its loop. How many degrees is each turn?

ModifyChange the code on purpose and check the result.

Modify

Change one number so the spiral grows faster.

Need a hint?

Only one number needs to change. Which number is multiplied by i?

Use the counter to make the line thicker on every pass.

Need a hint?

pensize( ) sets how thick the line is. The pen needs a number that changes each pass.

Another hint

The counter changes each pass, so use it. pensize(i + 1) starts at 1.

Try it yourselfYour edits are saved in this browser.

Use the counter to change the pen size.

    Run your program. This ticks when your code does all of the above.

    For teachers

    Students use the loop counter i to work out something different on every pass, and meet the fact that counting starts at 0. About 30 minutes.

    Open the teacher guide: answers, solutions, slides and a worksheet.

    Students will be able to

    Before class

    Run the spiral, then try pensize(i + 1) yourself. Notice the first line is invisible because i is 0.

    Running the lesson

    1. Predict: show the loop with forward(i * 5) and ask what it draws. Take a vote before anyone runs it; the wrong options are a square (the counter does not change), a circle, and an error because i was never stored.
    2. Run: students run it and compare. Ask: how long is the first line, and why?
    3. Investigate: trace the counter 0, 1, 2, 3 and say that counting starts at 0, because later lessons rely on it. The second task checks that a name still holds a value you can calculate with.
    4. Modify: students change the multiplier, then use the counter for the pen. Let them try pensize(i) first: it starts at 0, so the first line is thin. That is a good moment to talk about starting at 0.

    The hard part (on purpose)

    Believing the counter is the same number every pass, or that it starts at 1. Ask what i is on the first pass.

    When students get stuck

    Stretch and support

    Stretch: Draw a spiral that turns 91 degrees instead of 90. What happens, and why?

    Support: Give the first three values of i * 5 and ask the student to continue the pattern before running.

    Where it leads

    Next lesson makes a value grow without the counter: a variable that adds to itself. Counters are used for scores, timers and animations. Class ideas: a spiral gallery with different multipliers.

    What each step is for

    Now you can
    Next lesson5. A value that growsA variable can change itself, so a value grows each time round a loop.