← Teacher guide

If and else

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.

Students will be able to

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

  1. 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.
  2. 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.
  3. 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.
  4. Modify: one number changes. Ask: which passes are true now?
  5. 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.
  6. 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.

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 seeA question to askThe 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

  1. 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).
  2. 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.
  3. 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.
  4. 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.
  5. 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 in range(6):
    if i < 4:
        pencolor("red")
    else:
        pencolor("blue")
    forward(60)
    right(60)

Make

from turtle import *

size = 10
pensize(5)
for i in range(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

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.