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?
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make your prediction first. Help opens once you've locked it in.
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 inrange(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?
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 inrange(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 inrange(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.
i
i % 3
0
0
1
2
3
4
5
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 inrange(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.
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.
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.
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.
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
A student reads i % 2 as 'half' or 'percent'. Ask: What is left over when 7 is divided by 2? Fix: % gives the remainder. 7 % 2 is 1, 8 % 2 is 0.
The third colour never appears, or appears in the wrong places. Ask: Which question does Python ask first? Fix: Put the i % 3 == 0 check first. Python stops at the first true answer.
A SyntaxError on the elif line. Ask: Does the elif line end in a colon, and is it lined up with the if? Fix: elif i % 2 == 0: lines up with if and ends in a colon.
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
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.
Now you can
Say what i % 2 gives and when it is 0.
Pick every second or every third pass of a loop.
Add a third choice with elif and say which check wins.