Programming techniques and object-oriented programming · A level · OCR H446 2.2.1, AQA 7517 4.1.1.1, Eduqas A500QS 1.4 · about 20 min
The A level data types, references and user-defined types, constants, and definite and indefinite iteration.
[1 mark]A variable holds the location in memory where a list of readings is stored, rather than the readings themselves. What is its data type?
[1 mark]What does this program print?
a = [1, 2] b = a b.append(3) c = list(a) c.append(4) print(a, len(c))
[1 mark]A loop has its condition tested at the end. What is always true of it?
[1 mark]Which are advantages of using a named constant such as SAFE_CM instead of typing 25 wherever it is needed?
Tick every answer that is true.
[1 mark]A robot must repeat 'drive 5 cm' until the wall is 25 cm away. The number of repeats is not known in advance. What kind of iteration is this?
[1 mark]Python has no character type. What type does it use to store a single character such as "F"? Give the Python type name.
Define a record type Reading with namedtuple and three fields: heading (an integer, 0, 90, 180 or 270), cm (the real number distance() returns) and clear (a Boolean, True when cm is more than the constant CLEAR_CM). Declare CLEAR_CM = 40 as a constant and use the name, not 40, in the comparison.
The robot starts facing heading 0. Take a reading, store it in an array, turn right 90 degrees, and repeat until there are four readings and the robot faces its starting direction. Then print each record with print(r), which shows Reading(heading=90, cm=51.0, clear=True), and finally print clear directions: <n>, the number of records whose clear field is True.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
from collections import namedtuple
Reading = namedtuple("Reading", ["heading", "cm", "clear"])
survey = []
# take four readings, one every 90 degrees
print("clear directions:", 0)Plan your program here, then type it in and press Run.
taken, holding the time of the reading from clock(). What type is it?r.cm = 0 on one of your records. What happens, and why might that be useful for sensor data?