Make a program choose which lines to run, using if and else.
A thermostat asks one question, over and over: is it too cold?
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.
Make your prediction first. Help opens once you've locked it in.
from turtle import *
pensize(6)
for i in range(6):
if i < 3:
pencolor("red")
else:
pencolor("blue")
forward(60)
right(60)
i is the counter from the spiral, and the new line, if i < 3:, asks a question about it on every pass. What do you think the turtle will draw?
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 |
from turtle import *
pensize(6)
for i in range(6):
if i < 3:
pencolor("red")
else:
pencolor("blue")
forward(60)
right(60)
from turtle import *
pensize(6)
for i in range(6):
if i < 3:
pencolor("red")
else:
pencolor("blue")
forward(60)
right(60)
Work out the condition and the colour for each pass.
One small slip here stops the whole program.
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.
Change the condition so four sides are red and two are blue. Run it to check.
Before a hint: what kind of stuck is this? Try a strategy first
Which number sets where the red stops and the blue starts?
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.
Now the decision reads a value that grows, like the spiral's size. Work it out on paper before you check.
from turtle import *
size = 10
pensize(5)
for i in range(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?
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.
from turtle import *
size = 10
pensize(5)
for i in range(12):
# Choose the colour here: red while size is small, blue once it is big.
forward(size)
right(90)
size = size + 10
Before a hint: what kind of stuck is this? Try a strategy first
Put an if / else at the top of the loop, before the forward line.
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.
A fridge should stay at 5 degrees or less. This thermometer draws the reading as a bar, red when the fridge is too warm. Try readings of 3 and 8. Then make the bar green when the fridge is fine, and change the rule so it turns red above 4 instead. It's the same rule as the micro:bit fridge alarm, drawn instead of flashed.
Lesson done. You've finished every step you need.
Students make a program choose between two paths with if and else, reading a condition that is true or false. About 35 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
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".
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.
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.
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.