On this page

Repeat

Computers are good at doing the same thing many times without getting bored or making a slip. This module shows how to say "do this again" once, and let Python do the repeating.

Before this, you can: update a variable with x = x + 1; say which indented lines belong to an if; use a variable inside a call's arguments.

By the end you will be able to read a loop and say how many times it runs and what its variable holds each time, write a loop that places shapes, and add up as you go.

The same line, five times

Five circles in a row:

from scimigo import canvas, circle

canvas(600, 400)
circle(100, 200, 20, color="orange")
circle(200, 200, 20, color="orange")
circle(300, 200, 20, color="orange")
circle(400, 200, 20, color="orange")
circle(500, 200, 20, color="orange")

This works. It also takes five lines to say one thing: fifty circles would need fifty lines, and changing the size would mean editing every one. The five lines are almost the same; only one number changes, and it changes by the same amount each time. That is the situation a loop is for.

for and range

Start with a loop that draws nothing:

for i in range(3):
    print("hop")
hop
hop
hop

Read the first line as: do the indented lines 3 times. Its parts, in order: the word for, a name (here i), the word in, range(3), and a colon. Under it, indented by four spaces, are the lines to repeat. This is the same rule you met with if: indentation says which lines belong.

Each time through the indented lines is called a turn of the loop.

The loop variable counts for you

The name after for is a variable, and Python gives it a new value on every turn:

for i in range(4):
    print(i)
0
1
2
3

Two things here surprise almost everyone. The counting starts at 0, not 1. And range(4) never reaches 4: it gives four values, 0, 1, 2 and 3, and then the loop is over.

turn i
1st 0
2nd 1
3rd 2
4th 3

So range(4) means four turns, and on the last one i is 3. You can call the variable anything; i is a habit, and a longer name is often clearer.

What is inside the loop?

Only the indented lines repeat. The first line after them that is not indented runs once, when the loop has finished:

for i in range(3):
    print("hop")
print("done")
hop
hop
hop
done

Indent that last line and it becomes part of the loop, and the program says something different:

for i in range(3):
    print("hop")
    print("done")
hop
done
hop
done
hop
done

The two programs differ by four spaces. When a loop does too much or too little, look at the indentation before anything else.

Using i to place things

A loop that printed the same word each time did not need i. A loop that draws does, because each shape belongs somewhere different. The trick is to work the position out from i:

for i in range(5):
    x = 100 + i * 100
    print(i, x)
0 100
1 200
2 300
3 400
4 500

On the first turn i is 0, so x is 100 + 0, which is 100. Each turn after that, i is one bigger and x is 100 bigger. Those are the five x positions from the top of this lesson. Now the row of circles is three lines, however many circles there are:

from scimigo import canvas, circle

canvas(600, 400)
for i in range(5):
    x = 100 + i * 100
    circle(x, 200, 20, color="orange")

In 100 + i * 100, the first 100 is where the row starts and the second is the gap between one circle and the next. To pack them closer, change the gap. To draw more, change the number in range.

Counting from somewhere else

range can be told where to start, and how big a step to take:

for n in range(2, 6):
    print(n)
2
3
4
5

With two arguments, range(start, stop) begins at start and stops before stop. With three, the last is the step:

for x in range(100, 600, 100):
    print(x)
100
200
300
400
500

Here the loop variable is the position itself, so no arithmetic is needed. The stop is 600 and the last value is 500: whatever the step, range gives the values that come before the stop and never the stop itself.

Counting with i and working out x, or letting range produce x directly, are two ways to write the same loop. Use whichever you find easier to read.

Adding up as you go

A loop can build up an answer over its turns. Put a variable before the loop, and update it inside:

total = 0
for i in range(1, 5):
    total = total + i
print(total)
10

total = total + i is the three moves from the last module, done once per turn: read total, add this turn's i, store the result back.

turn i total after the turn
1st 1 1
2nd 2 3
3rd 3 6
4th 4 10

total = 0 has to come before the loop. Inside it, the total would be set back to nothing at the start of every turn. And print(total) is not indented, so it runs once at the end. Indented, it would print the total after every turn, which is sometimes exactly what you want.

You will often see total = total + i written as total += i. It means the same thing.

In the lab

You will say how many times a loop runs and what its variable holds before you run it, turn five circles into eight, draw a staircase where every stair is worked out from the loop variable, and chart twelve weeks of savings.

Your turn

Now try it yourself

Predict what a program will do, run it, and step through it line by line. The checks tell you when your own code works.

Loading the lab…

In this lab

  1. How many times?

    Read the program. Before you run it, choose what it prints.

  2. What is i?

    This loop prints its own loop variable. Choose the list of values it prints, one per turn.

  3. Inside or after?

    These two programs differ only in how far one line is pushed in. Decide what each one prints.

  4. Five circles to eight

    This program draws five circles. Change it so that it draws eight, and so that the centres are 70 apart instead of 60. The first circle stays where it is.

    Answer the question first, then make the change and see whether the picture agrees with you.

  5. Start, stop, step

    range can be told where to start and how big a step to take. For each loop, choose the values it prints.

  6. Ten circles

    Draw ten orange circles in a row. Every circle has radius 15 and sits at y = 200. Their centres are at x = 50, 100, 150 and so on, up to 500.

  7. Adding up

    This loop keeps a running total. Choose the table that shows total after each turn, then say what is printed.

  8. Staircase

    Draw steps stairs climbing to the right.

    • Each stair is a rectangle 50 wide.
    • The first stair is 30 tall. Each stair is 30 taller than the one before it.
    • All the stairs stand on the ground line, y = 350.
    • The first stair's left edge is at x = 40, and each stair starts where the last one ended.

    A rectangle is placed by its top-left corner, so a taller stair has to start higher up. The checks try your program with other numbers of stairs.

  9. Savings

    You save weekly every week for weeks weeks, starting with nothing.

    • After each week, print your balance: one number on each line.
    • Draw one bar for each week. A bar is 30 wide and as many pixels tall as the balance that week. Bars stand on y = 380. The first bar's left edge is at x = 40 and each bar starts 40 further right.

    The checks try other amounts and other numbers of weeks.

  10. Fence

    Draw a fence with posts posts.

    • A post is a rectangle 10 wide and 80 tall, standing on y = 300.
    • The first post's left edge is at x = 60, and each post is 60 further right than the last.
    • Two rails join the posts. Each rail is a rectangle 8 tall. A rail starts at the left edge of the first post and ends at the right edge of the last post. Put one rail at y = 235 and the other at y = 270.

    The rails are not repeated, so think about whether they belong in the loop. Their length depends on posts.

  11. Odd numbers

    Print the sum of the odd numbers from 1 up to last. With last = 99 that is 1 + 3 + 5 and so on up to 99.

    Print one line: the sum. There is no picture in this one. The checks try other values of last, all of them odd.

  12. One too few

    This program should draw twelve seats, numbered 1 to 12. Run it and count. Then fix it by changing one number.

    Before you change anything, say to yourself which seat number the loop never reaches, and why.

  13. Nested squares

    Draw squares squares that share one centre, the point (300, 200).

    • The largest is 240 wide. Each of the others is 40 narrower than the one before.
    • Draw the largest first, so that every square stays visible.
    • All but the last alternate blue and light blue, starting with "blue": "blue", "lightblue", "blue" and so on.
    • The last and smallest is "orange".

    With squares = 6 that is five alternating squares and an orange one 40 wide. The checks try other numbers of squares too, so the loop has to follow squares, and so does the size of the orange square.

    One way to alternate: keep this turn's colour and the next turn's colour in two variables, and swap them after each square. A swap needs a third variable to hold one value for a moment; the homework task "The swap that loses a value" in Values and Variables shows why. Draw the orange square with its own line after the loop.

  14. Skyline

    Draw a city at the edge of a lake.

    buildings towers stand side by side on the ground line. They are not all the same height: the height follows a rule that you choose, which depends on each tower's position in the row. One tower, the tallest under your rule, has a red light on its roof. If hour is before 18 it is day: the sky is light and there is a sun. Otherwise it is night: the sky is dark and there is a moon. Below the ground line is the lake.

    There are no steps this time. So that your picture can be checked, keep to these:

    • The sky is the first shape you draw: a rectangle that covers the whole canvas, "skyblue" by day and "midnightblue" by night.
    • The sun is a "gold" circle. The moon is a "lightyellow" circle. Only one of them is drawn.
    • The ground line is y = 320. A tower is a rectangle 40 wide whose bottom edge is on the ground line.
    • The lake is a rectangle below the ground line.

    Everything else is yours to decide: the rule for the heights, the gaps, the colours, where the sun and moon go. Choose a rule for which you can tell, from buildings alone, which tower is tallest.