The worksheetDownload the PDF
Answers

F2.3 else, elif and Boolean operators

Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.5, Edexcel 1CP2 1.2.3 · about 15 min

BugBotLab

What this lesson is about

Choosing between outcomes, and joining conditions with and, or and not.

Questions 8 marks in all

  1. [1 mark]What does this program print?

    d = 30
    if d < 20:
        print("stop")
    elif d < 40:
        print("careful")
    else:
        print("go")
    Answer:
    careful

    Python checks from the top and runs the first block whose question is True: 30 < 20 is False, 30 < 40 is True.

  2. [1 mark]With an if and an else, how many of the two blocks run?

    1. AExactly one
    2. BBoth
    3. CNeither
    4. DIt depends on the program
    Answer: A. The if block when the question is True, the else block when it is False. Never both, never neither.
  3. [1 mark]What does this program print?

    print(True and False)
    print(True or False)
    print(not True)
    Answer:
    False
    True
    False

    and needs both sides True, or needs at least one, and not flips the answer.

  4. [1 mark]The traffic light checks d < 40 first and d < 20 second, with elif. What goes wrong when d is 10?

    1. AIt says careful instead of stop: the first True question wins
    2. BNothing goes wrong
    3. CIt says both
    4. DAn error
    Answer: A. 10 < 40 is True, so that block runs and the rest are skipped. With elif, the order of the questions matters.
  5. [1 mark]What does this program print?

    d = 50
    b = 10
    if d > 30 and b > 20:
        print("drive")
    else:
        print("stay")
    Answer:
    stay

    d > 30 is True but b > 20 is False, and and needs both.

  6. [1 mark]What is elif short for? (two words)

    Answer: else if. elif means else if: checked only when the questions above it were False.
  7. [1 mark]In a truth table, when is A OR B False?

    1. AOnly when A and B are both False
    2. BWhen either is False
    3. CWhen both are True
    4. DNever
    Answer: A. OR needs at least one side True, so it is False only when both sides are False.
  8. [1 mark]What does this program print?

    d = 25
    if d >= 20 and d <= 40:
        print("in the zone")
    else:
        print("outside")
    Answer:
    in the zone

    25 is at least 20 and at most 40, so both sides are True.

The task: traffic light

Drive 15 cm, then read the distance and set the LED: red and print stop if the wall is under 20 cm away; orange and careful if under 40; green and go otherwise. The wall is placed so that the right answer is orange, but write all three branches.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

forward(50, distance=15)
d = distance()
if d < 20:
    led("red")
    print("stop")

The hint students can ask for: Drive first, then read the distance once into a variable and test it. Write all three branches, even though only one of them can run today.

A solution

from bugbot import *
connect()
forward(50, distance=15)
d = distance()
if d < 20:
    led('red')
    print('stop')
elif d < 40:
    led('orange')
    print('careful')
else:
    led('green')
    print('go')

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.