Make a program choose which lines to run, using if and else.
Traffic lights, game scores, login screens and thermostats all do the same thing: check a condition and pick what happens next.
So far every line of your program has run, every time. Now the program will choose: run some lines if something is true, and other lines if it is not.
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: Build something new using what you have learned. Expect a few tries; that is normal.
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
decisions.py
from turtle import *
pensize(6)
for i inrange(6):
if i < 3:
pencolor("red")
else:
pencolor("blue")
forward(60)
right(60)
i is the counter from the spiral. The new line is if i < 3:. Read the code. What will the turtle draw?
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 < 3:
Asks: is i less than 3?
The answer is True or False
pencolor("red")
Runs only when the answer is True
It is indented under the if
else:
Means "otherwise"
Its lines run only when the answer is False
pencolor("blue")
Runs only when the answer is False
Only one of the two paths runs on each pass
decisions.py
from turtle import *
pensize(6)
for i inrange(6):
if i < 3:
pencolor("red")
else:
pencolor("blue")
forward(60)
right(60)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.
Investigate
decisions.py
from turtle import *
pensize(6)
for i inrange(6):
if i < 3:
pencolor("red")
else:
pencolor("blue")
forward(60)
right(60)
Work out the condition and the colour for each pass.
Here is a version where something is wrong.
if asks a question, and Python works out the answer before deciding.
Fill in the gaps for each pass: is i < 3, and which colour does the program pick?
i
i < 3
colour
0
True
red
2
3
5
This should colour only the fourth side red, but the program will not even start. Click the wrong line.
ModifyChange the code on purpose and check the result.
Modify
Change the condition so four sides are red and two are blue. Run it to check.
Try it yourselfYour edits are saved in this browser.
Need a hint?
Which number sets where the red stops and the blue starts?
Another hint
i is 0, 1, 2, 3 for four passes. Which test is true for exactly those?
Make four sides red and two blue.
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
This mixes two lessons: a value that grows, and a decision that reads it. Work it out on paper first.
decisions.py
from turtle import *
size = 10pensize(5)
for i inrange(12):
# Choose the colour here: red while size is small, blue once it is big.forward(size)
right(90)
size = size + 10
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?
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
Make
The spiral grows by 10 every pass. Make it red while the lines are short and blue once they are long. Then try a different cut-off, or a third colour of your own once you have seen elif next lesson.
Try it yourselfYour edits are saved in this browser.
Need a hint?
Put an if / else at the top of the loop, before the forward line.
Another hint
The condition reads size: if size < 60, the pen is red; else it is blue.
Colour the spiral with an if / else that reads size.
Run your program. This ticks when your code does all of the above.
For teachers
Students make a program choose between two paths with if and else, reading a condition that is true or false. About 35 minutes.
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.
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.
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
A SyntaxError on the if line. Ask: Does the condition ask a question (==, <) or store a value (=)? Is there a colon at the end? Fix: Use == or < to compare, and end the line with a colon: if i < 3:
Every side is the same colour. Ask: Is the if / else inside the loop, so it is asked again every pass? Fix: Indent the if / else under the for line so it runs once per pass.
The spiral is all red or all blue. Ask: What is size on the first pass and on the last? Is the cut-off between them? Fix: The cut-off must be a number size passes through, for example 60.
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.
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.
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.
Now you can
Write an if / else that chooses between two actions.
Work out whether a condition is true for a given value.
Say that only one of the two paths runs on each pass.