← Python Foundations with Turtle

If and else

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.

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 in range(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?

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 in range(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 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.

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?

ii < 3colour
0Truered
2
3
5

This should colour only the fourth side red, but the program will not even start. Click the wrong line.

A condition works out to a value, like a sum does. What does 5 < 3 work out to?

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 = 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?

    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.

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      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.

      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

      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

      Now you can
      Next lesson8. Every otherUse the remainder, %, to pick every second or third pass, and add a third choice with elif.