← Teacher guide

Every other

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

Python Foundations with Turtle · Choices and lists · about 30 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 10 min · Modify 7 min · Mix it up 5 min

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.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

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

What you will seeA question to askThe fix
A student reads i % 2 as 'half' or 'percent'.What is left over when 7 is divided by 2?% gives the remainder. 7 % 2 is 1, 8 % 2 is 0.
The third colour never appears, or appears in the wrong places.Which question does Python ask first?Put the i % 3 == 0 check first. Python stops at the first true answer.
A SyntaxError on the elif line.Does the elif line end in a colon, and is it lined up with the if?elif i % 2 == 0: lines up with if and ends in a colon.

Answers to the checks

  1. Predict: What colour are the sides, in order?
    Red and blue take turns. i % 2 is the remainder after dividing i by 2: 0 for 0, 2, 4 and 6, and 1 for 1, 3, 5 and 7. The condition is true on every second pass.
  2. Follow the remainder: Fill in the gaps: the remainder when i is divided by 3.
    1, 2, 0, 1, 2
    Dividing by 3 leaves a remainder of 0, 1 or 2, then it starts again. i % 3 == 0 is true for 0, 3, 6, 9: every third pass.
  3. What does % give?: What does 7 % 2 work out to?
    1
    2 fits into 7 three times (6), and 1 is left over. So 7 % 2 is 1.
  4. Count the red sides: The loop runs for i = 0 to 9 (ten passes). How many are red with if i % 2 == 0?
    5
    The red passes are i = 0, 2, 4, 6 and 8, so there are five.
  5. Which check wins?: On pass i = 6, both i % 3 == 0 and i % 2 == 0 are true. Which colour is the side?
    Green: the first true check wins
    Python asks the checks in order and stops at the first true one. 6 % 3 == 0 is asked first, so the side is green and the elif is never asked.
  6. Work out the length: size = 30 and the line is forward(size + (i % 2) * 20). How long is the line on pass i = 3?
    50
    3 % 2 is 1, so the line is 30 + 1 * 20 = 50. On even passes the remainder is 0, so the line is 30.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

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)

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.

Exit ticket

A loop colours pass i red if i % 2 == 0, else blue. What colour is pass 5, and why?

Blue. 5 % 2 is 1, so the condition is false and the else path runs.

What each step is for

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.