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.
SlidesDownload Present from the browser, or export to PowerPoint or PDF.
WorksheetDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
I can say what i % 2 gives and when it is 0.
I can pick every second or every third pass of a loop.
I can add a third choice with elif and say which check wins.
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
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.
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?
Investigate: students fill in the remainder table before running. Say that remainders repeat, and that the repeat is what makes a pattern.
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.
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.
By the end of this lesson(start)
Here is the finished program running. By the end of the lesson you will write one like it.
Run the finished example on the projector before anything else.
Stand on the remainder(investigate, 5 min)
Number students 0 to 7. Ask those with i % 2 == 0 to stand, then i % 3 == 0. Which students stand for both?
Make the idea physical before it goes back to the screen.
Guess the rule(end, 3 min)
Leave your stripe pattern on screen. The next pair guesses its rule from the picture, then reads your if line to check.
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 see
A question to ask
The 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
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.
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.
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.
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.
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.
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 inrange(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
Investigate: The remainder repeats: 0, 1, 0, 1 for 2 and 0, 1, 2, 0, 1, 2 for 3. That repeat is what makes a pattern.
Modify: A second question (elif) is asked only when the first answer was no, so it adds a choice without rewriting the rest.
Mix it up: When an if and an elif could both be true, only the first one runs.
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.