Functions and structured code · GCSE · OCR J277 2.1.1, AQA 8525 3.1.1, Edexcel 1CP2 1.1.1 · about 20 min
Structure diagrams, and hiding the details of a job behind a function.
[1 mark]What is decomposition?
[1 mark]What is abstraction?
[1 mark]A simulator's mat keeps the walls and distances but not the carpet colour. Which idea is that?
[1 mark]What does a structure diagram show?
[1 mark]Why keep a delivery route as a list of stops, separate from the code that drives?
Write go_to(x, y) and signal(), then use them to visit three stops in order, signalling at each: A at (30, 10), B at (30, 60) and C at (0, 60), all in cm from where the robot starts. The robot starts in the bottom left of the mat.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def go_to(x, y):
"""Drive to (x, y), in cm from where the robot started."""
here_x, here_y = position()
def signal():
"""Light and beep at a stop."""
led("green")Plan your program here, then type it in and press Run.
go_to?go_to return how far it drove in total, and print the length of the whole route.