Programming techniques and object-oriented programming · A level · AQA 7517 4.1.1.3, Eduqas A500QS 1.4 · about 20 min
Integer division and MOD with negatives, rounding and truncation, XOR, string and date conversions, and pseudo-random numbers.
[1 mark]What does this program print?
print(-17 // 5, -17 % 5)
[1 mark]What does this program print?
import math x = -2.6 print(round(x), int(x), math.floor(x))
[1 mark]What is True XOR True?
[1 mark]What does this program print?
s = "R135,F20"
comma = s.find(",")
print(comma, int(s[1:comma]) + 1, s[comma + 1:] + "!")[1 mark]What does this program print?
heading = 30
for turn in [-90, -45, 200]:
heading = (heading + turn) % 360
print(heading)[1 mark]Why does setting the same seed before generating numbers give the same sequence each time?
A route arrives as one string of commands separated by commas: COMMANDS = "F20,L90,F15,R135,B5". Each command is a letter and a whole number: F drives forward that many cm, B drives backward that many cm, R turns right that many degrees and L turns left that many degrees.
Go through the string and carry out each command on the robot. Keep two totals as you go: the distance driven in cm (forward and backward both add) and the heading in degrees, starting at 0, turning right adding and turning left subtracting, always kept from 0 to 359 with MOD 360. Convert each amount with int. At the end print driven <cm> cm, heading <degrees>, for this string driven 40 cm, heading 45. Work both totals out from the string; do not type them.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
COMMANDS = "F20,L90,F15,R135,B5"
for command in COMMANDS.split(","):
print(command)Plan your program here, then type it in and press Run.
split, finding each comma with find and taking substrings between them.-7 // 2, -7 % 2 and int(-7 / 2) give? Explain why two of them differ.W that waits a number of tenths of a second, so W15 waits 1.5 seconds.