Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.5, Edexcel 1CP2 1.2.3 · about 15 min
Choosing between outcomes, and joining conditions with and, or and not.
[1 mark]What does this program print?
d = 30
if d < 20:
print("stop")
elif d < 40:
print("careful")
else:
print("go")careful
Python checks from the top and runs the first block whose question is True: 30 < 20 is False, 30 < 40 is True.
[1 mark]With an if and an else, how many of the two blocks run?
[1 mark]What does this program print?
print(True and False) print(True or False) print(not True)
False True False
and needs both sides True, or needs at least one, and not flips the answer.
[1 mark]The traffic light checks d < 40 first and d < 20 second, with elif. What goes wrong when d is 10?
[1 mark]What does this program print?
d = 50
b = 10
if d > 30 and b > 20:
print("drive")
else:
print("stay")stay
d > 30 is True but b > 20 is False, and and needs both.
[1 mark]What is elif short for? (two words)
[1 mark]In a truth table, when is A OR B False?
[1 mark]What does this program print?
d = 25
if d >= 20 and d <= 40:
print("in the zone")
else:
print("outside")in the zone
25 is at least 20 and at most 40, so both sides are True.
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.
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.