The worksheetDownload the PDF
Answers

F2.2 Selection: if

Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.4, Edexcel 1CP2 6.5.2 · about 12 min

BugBotLab

What this lesson is about

Relational operators, and a block that runs only when a condition is true.

Questions 9 marks in all

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

    speed = 40
    if speed > 50:
        print("fast")
    if speed > 30:
        print("medium")
    if speed > 10:
        print("slow")
    print("checked")
    Answer:
    medium
    slow
    checked

    Three separate ifs are each checked. 40 > 50 is False; 40 > 30 and 40 > 10 are both True.

  2. [1 mark]What is the difference between = and ==?

    1. A= stores a value, == asks whether two values are equal
    2. BThey mean the same
    3. C== stores a value, = compares
    4. D= is for text, == for numbers
    Answer: A. Mixing them up is one of the most common early mistakes.
  3. [1 mark]Which comparison means "is not equal to"?

    1. A!=
    2. B<>
    3. C=!
    4. Dnot=
    Answer: A. != is not equal. The others are not Python.
  4. [1 mark]What does this program print?

    d = 25
    print(d < 30)
    print(d >= 30)
    Answer:
    True
    False

    A comparison gives True or False.

  5. [1 mark]What happens to the block under an if when the question is False?

    1. AIt is skipped, and the program carries on below it
    2. BIt runs anyway
    3. CThe program stops
    4. DPython shows an error
    Answer: A. The block runs only when the answer is True.
  6. [1 mark]What does this program print?

    x = 7
    if x % 2 == 0:
        print("even")
    print("done")
    Answer:
    done

    7 % 2 is 1, so the question is False and the block is skipped.

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

    answer = "Yes"
    if answer == "yes":
        print("driving")
    print("done")
    Answer:
    done

    "Yes" with a capital Y is a different string from "yes", so the condition is False.

  8. [1 mark]d is exactly 40. Which condition is True?

    1. Ad <= 40
    2. Bd < 40
    3. Cd > 40
    4. Dd != 40
    Answer: A. Only the or-equal version includes 40 itself. The edge value is where selection mistakes hide.
  9. [1 mark]How does AQA pseudo-code write "is equal to"?

    1. A=
    2. B==
    3. C===
    4. DEQUALS
    Answer: A. AQA pseudo-code uses a single = for equality (and ← for assignment). Python uses ==.

The task: near or far

Drive 20 cm. Then, with two separate ifs: if the wall is less than 40 cm away, print near and make the LED red; if it is 40 or more, print far and make it green. The wall has been placed so the answer should be near, but write both.

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

forward(50, distance=20)
d = distance()
if d < 40:
    print("near")

The hint students can ask for: Drive 20 cm. Then use two ifs: if the wall is under 40 cm away print 'near' and make the LED red; if it is 40 or more, print 'far' and make it green.

A solution

from bugbot import *
connect()
forward(50, distance=20)
d = distance()
if d < 40:
    print('near')
    led('red')
if d >= 40:
    print('far')
    led('green')

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