Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.2, Edexcel 1CP2 6.2.2 · about 15 min
Decisions inside decisions, and choosing by exact value with a case statement.
[1 mark]What does this program print?
d = 60
b = 10
if d < 30:
if b < 20:
print("stuck and flat")
else:
print("stuck")
else:
if b < 20:
print("free but flat")
else:
print("free")[1 mark]Which else does an else belong to in nested selection?
[1 mark]What does this program print?
command = "left"
match command:
case "forward":
print("f")
case "left" | "right":
print("sideways")
case _:
print("unknown")[1 mark]What does case _: do in a match?
[1 mark]OCR's Exam Reference Language writes a case statement with which keyword? (one word)
[1 mark]Which is the same as if a > 30: if b > 50: print("go") (with no else parts)?
if a > 30 and b > 50: print("go")if a > 30 or b > 50: print("go")if not a > 30: print("go")if a > 30: print("go")The robot asks Which way? and then How far? . Use match (or elif) on the direction to drive that far: forward, back, left or right. For any other word, print unknown and do not move. The task answers right and 20; your program must work for any answers, so do not type them in.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
command = input("Which way? ")
far = float(input("How far? "))Plan your program here, then type it in and press Run.
stop command that turns the LED red and does not move.if inside that case.elif instead of match. Which is easier to read?