Robust programs · GCSE · about 25 min
A command program that authenticates, validates every command, and is tested against a plan.
[1 mark]What does this program print?
def parse(command):
parts = command.strip().lower().split()
if len(parts) != 2 or parts[0] not in ["forward", "back"]:
return None
if not parts[1].isdigit() or not 1 <= int(parts[1]) <= 50:
return None
return (parts[0], int(parts[1]))
print(parse("Forward 20"), parse("forward 60"), parse(""))[1 mark]Why put all the command checks in one function, parse?
[1 mark]In the controller's test plan, an empty command is which kind of test data?
[1 mark]Which comes first in the controller, and why?
Build the controller from the brief with operators = {"ada": "robot42"}. Print access denied for a wrong login, welcome <name> for a right one, invalid command for every command it refuses, and bye on quit. The task types: ada and oops, then ada and robot42, then the commands forward 20, fly 10, right 500, an empty line, right 15, beep and quit.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
operators = {"ada": "robot42"}
MOVES = ["forward", "back", "left", "right"]Plan your program here, then type it in and press Run.
forward command that would take the robot closer than 10 cm to a wall, using distance().quit.