← Python Foundations with Turtle

Every other

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

Can a loop make a pattern that repeats, like the squares on a chessboard?

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?

How this lesson works: Predict, Run, Investigate, Modify
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 %, and it doesn't mean percent: i % 2 is the remainder when you divide i by 2. What colours do you think the sides will be, 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.

Try one in your head first.

You can answer this one without drawing anything.

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.
Build up to it
Side 6 is a third side and an even one. You want it green. Which check has to come first?

Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

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)
    

    It comes down to the order Python asks its questions in.

    This one needs a name, the counter and the remainder all at once.

    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?

    ModifyChange the code on purpose and check the result.

    Change a zebra crossing

    A zebra crossing is white and grey taking turns. Make the crossing longer, with 12 stripes. Then use elif to make the first stripe yellow, so drivers see where it starts: the first stripe is i == 0.

    Try it yourselfYour edits are saved in this browser.
    Now you can
    Next lesson9. ListsKeep many values under one name, pick one by its place, and count them with len.
    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