The answersDownload the PDF
Worksheet

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
NameClassDate

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")
  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
  3. [1 mark]What does this program print?

    print(True and False)
    print(True or False)
    print(not True)
  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
  5. [1 mark]What does this program print?

    d = 50
    b = 10
    if d > 30 and b > 20:
        print("drive")
    else:
        print("stay")
  6. [1 mark]What is elif short for? (two words)

  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
  8. [1 mark]What does this program print?

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

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")

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/f2-3-else-elif-and-boolean-operators/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Add a fourth colour for "very far" (over 80 cm) to the traffic light. Where must its condition go?
  2. Only drive if the battery is above 50 as well, using and.
  3. Rewrite safe-to-go so the blocked case comes first, using not.