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)
It comes down to the order Python asks its questions in.
This one needs a name, the counter and the remainder all at once.
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.
Lesson done. You've finished every step you need.
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.
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.