← Python Foundations with Turtle

Every other

Use the remainder, %, to pick every second or third pass, and add a third choice with elif.

Zebra crossings, chessboards, stripes and blinking lights all repeat a pattern: every second one is different.

Last lesson the program chose by comparing the counter with a number. A pattern that repeats needs a different question: is this pass an even one?

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

every_other.py
from turtle import *

pensize(6)
for i in range(8):
    if i % 2 == 0:
        pencolor("red")
    else:
        pencolor("blue")
    forward(30)
    right(45)

The new sign is %. It does not mean percent: i % 2 is the remainder when i is divided by 2. Read the code. What colour are the sides, in order?

What colour are the sides, in order?

RunRun it and compare the result with your prediction.

Run

Run it and compare it with your prediction.

Line What it does Why
if i % 2 == 0: Is the remainder 0 when i is divided by 2? True for 0, 2, 4, 6: every second pass
pencolor("red") Runs on those passes The even ones
else: Means "otherwise" The odd passes: 1, 3, 5, 7
every_other.py
from turtle import *

pensize(6)
for i in range(8):
    if i % 2 == 0:
        pencolor("red")
    else:
        pencolor("blue")
    forward(30)
    right(45)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

every_other.py
from turtle import *

pensize(6)
for i in range(8):
    if i % 2 == 0:
        pencolor("red")
    else:
        pencolor("blue")
    forward(30)
    right(45)

Work out the remainder for each pass.

Predict the answer to one remainder.

Use the pattern to answer without drawing.

Fill in the gaps: the remainder when i is divided by 3.

ii % 3
00
1
2
3
4
5

What does 7 % 2 work out to?

The loop runs for i = 0 to 9 (ten passes). How many are red with if i % 2 == 0?

ModifyChange the code on purpose and check the result.

Modify

Make every third side green and keep the red and blue taking turns on the rest. Use elif.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Put the new check first, before the others. Python stops at the first true answer.

Another hint

i % 3 == 0 is true for 0, 3 and 6. Use elif for the red check.

Make every third side green, using elif.

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

    InvestigateTrace how each line of code connects to what you see.

    Mix it up

    every_other.py
    from turtle import *
    
    pensize(6)
    for i in range(8):
        if i % 3 == 0:
            pencolor("green")
        elif i % 2 == 0:
            pencolor("red")
        else:
            pencolor("blue")
        forward(30)
        right(45)
    

    Think about the order Python asks the questions.

    Combine a name, the counter and the remainder.

    On pass i = 6, both i % 3 == 0 and i % 2 == 0 are true. Which colour is the side?

    size = 30 and the line is forward(size + (i % 2) * 20). How long is the line on pass i = 3?

    For teachers

    Students use the remainder, %, to make a decision repeat every second or third pass, and add a third choice with elif. About 30 minutes.

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

    Students will be able to

    Before class

    The new symbol is %, the remainder after dividing, not "percent". 7 % 2 is 1 (7 ÷ 2 is 3, remainder 1). Run the pattern and try i % 3 yourself.

    Running the lesson

    1. Predict: show the loop and take a vote. The wrong options are each a slip about what the condition picks: a half, everything, or only the first pass.
    2. Run: run it live, then students run it. Say the if line as a question: is the remainder of i divided by 2 equal to 0?
    3. Investigate: students fill in the remainder table before running. Say that remainders repeat, and that the repeat is what makes a pattern.
    4. Modify: elif is a second question, asked only if the first answer was no. The order matters, so ask what happens if the i % 3 check goes last.
    5. Mix it up: the first question is about which check wins when two are true. The second joins a name, the counter and the remainder.

    The hard part (on purpose)

    Reading i % 2 as 'half' and the order of elif. Ask them to work out the remainder for i = 0, 1, 2, 3 before running.

    When students get stuck

    Stretch and support

    Stretch: Make a pattern that repeats every 4 sides with four colours, using % 4.

    Support: Give the remainder table with the first rows done, and ask the student to say each condition aloud before filling in the colour.

    Where it leads

    Next lesson keeps many values under one name, a list, so a pattern can pick from colours you choose. The remainder returns in Catch the turtle.

    What each step is for

    Now you can
    Next lesson9. ListsKeep many values under one name, pick one by its place, and count them with len.