Students make a program choose between two paths with if and else, reading a condition that is true or false.
Python Foundations with Turtle · Choices and lists · about 35 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 write an if / else that chooses between two actions.
I can work out whether a condition is true for a given value.
I can say that only one of the two paths runs on each pass.
Timing
Predict and Run 10 min · Investigate 10 min · Modify 5 min · Mix it up 3 min · Make 7 min
Before class
Run the red and blue hexagon, then change i < 3 to i < 4. Notice that only the number in the condition changed. < means "less than".
Running the lesson
Predict: show the loop and take a vote before anyone runs it. The wrong options are three slips: both paths run on every pass, the sides take turns, and the else covers everything.
Run: run it live, then students run it. Read the if line aloud as a question: is i less than 3? Yes runs the lines under if; no runs the lines under else.
Investigate: students fill in the trace table before running anything. The bug task is the classic slip: = stores, == asks. The last question shows that a condition works out to True or False, like a sum works out to a number.
Modify: one number changes. Ask: which passes are true now?
Mix it up: students work the number out on paper. It joins a growing value to a decision, so the condition reads size, not i.
Make: the colour depends on the line length. Check that the if / else is inside the loop, so it is asked again on every pass.
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.
Be the condition(investigate, 5 min)
Students stand when the condition is true for their pass number. i < 3: who stands? Then change the number.
Make the idea physical before it goes back to the screen.
The hard part (on purpose)
Writing a condition: the comparison sign, the colon, and the idea that only one path runs. Ask them to work out the condition for i = 0, 1, 2 before running.
When students get stuck
What you will see
A question to ask
The fix
A SyntaxError on the if line.
Does the condition ask a question (==, <) or store a value (=)? Is there a colon at the end?
Use == or < to compare, and end the line with a colon: if i < 3:
Every side is the same colour.
Is the if / else inside the loop, so it is asked again every pass?
Indent the if / else under the for line so it runs once per pass.
The spiral is all red or all blue.
What is size on the first pass and on the last? Is the cut-off between them?
The cut-off must be a number size passes through, for example 60.
Answers to the checks
Predict: What will the turtle draw? Three red sides, then three blue. On each pass the program asks one question, i < 3, and runs only one of the two paths. i is 0, 1, 2 for the first three passes (red) and 3, 4, 5 for the last three (blue).
Trace the passes: Fill in the gaps for each pass: is i < 3, and which colour does the program pick? True, red, False, blue, False, blue The condition is true for 0, 1 and 2, so the if path runs and the pen is red. From 3 on it is false, so the else path runs and the pen is blue.
Spot the bug: This should colour only the fourth side red, but the program will not even start. Click the wrong line. line 5 A single = stores a value. To ask whether two things are equal, the condition needs two: i == 3.
True or false: A condition works out to a value, like a sum does. What does 5 < 3 work out to? False 5 < 3 is a question, and the answer is False. Every condition works out to True or False, and if runs its lines only when it is True.
Mix it up: size starts at 10 and grows by 20 every pass. A line is red while size < 50, otherwise blue. How many of the first six lines are red? 2 size is 10, 30, 50, 70, 90, 110. Only 10 and 30 are less than 50, so two lines are red.
Solutions
One possible solution for each task. Other correct programs are fine.
Modify
from turtle import *
pensize(6)
for i inrange(6):
if i < 4:
pencolor("red")
else:
pencolor("blue")
forward(60)
right(60)
Make
from turtle import *
size = 10pensize(5)
for i inrange(12):
if size < 60:
pencolor("red")
else:
pencolor("blue")
forward(size)
right(90)
size = size + 10
Stretch and support
Stretch: Colour the first half of the lines red and the second half blue without typing the number 6 anywhere in the condition.
Support: Give the trace table with the first two 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, else blue. For range(5), how many passes are blue?
Three. The red passes are i = 0 and 1, so 2, 3 and 4 are blue.
What each step is for
Investigate: A decision is a condition plus two paths. You can follow it by working out the condition for each value, and find a bug by checking the form of the condition.
Modify: Changing a condition changes which passes take which path, and nothing else in the program.
Mix it up: A decision can use any name, not only the counter. Follow the value that the condition reads.
Where it leads
Next lesson uses the remainder, %, to make a pattern that repeats every second or third pass, and adds a third choice with elif. Decisions return later inside functions and in the game's hit test.