Strings, lists and records · GCSE · OCR J277 2.2.3, AQA 8525 3.2.6, Edexcel 1CP2 6.3.1 · about 15 min
Grouping related fields: a sensor reading as a record, and a list of records as a table.
[1 mark]What makes a record different from an array?
[1 mark]What does this program print?
reading = {"heading": 90, "distance": 51.0}
reading["heading"] = 180
print(reading["heading"], reading["distance"])[1 mark]What does this program print?
log = [{"h": 0, "d": 31}, {"h": 90, "d": 51}, {"h": 180, "d": 15}]
best = log[0]
for r in log:
if r["d"] > best["d"]:
best = r
print(best["h"])[1 mark]A robot log stores a time, a heading, a distance and a note for each reading. Which data types suit the fields?
Tick every answer that is true.
[1 mark]In a table of records, what is each row?
The robot is boxed in on three sides. Look in four directions, 90 degrees apart, storing each look as a record with the fields heading and distance in a list. Then print one line per record as heading 0: 31.0 cm, and finally print closest heading: <heading> for the record with the smallest distance.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
log = []
for i in range(4):
turn_right(30, angle=90)Plan your program here, then type it in and press Run.
time field to each record using clock().{r['distance']:6}.