The answersDownload the PDF
Worksheet

A1.2 Operations, strings and random numbers

Programming techniques and object-oriented programming · A level · AQA 7517 4.1.1.3, Eduqas A500QS 1.4 · about 20 min

BugBotLab
NameClassDate

What this lesson is about

Integer division and MOD with negatives, rounding and truncation, XOR, string and date conversions, and pseudo-random numbers.

Questions 6 marks in all

  1. [1 mark]What does this program print?

    print(-17 // 5, -17 % 5)
  2. [1 mark]What does this program print?

    import math
    x = -2.6
    print(round(x), int(x), math.floor(x))
  3. [1 mark]What is True XOR True?

    1. AFalse
    2. BTrue
  4. [1 mark]What does this program print?

    s = "R135,F20"
    comma = s.find(",")
    print(comma, int(s[1:comma]) + 1, s[comma + 1:] + "!")
  5. [1 mark]What does this program print?

    heading = 30
    for turn in [-90, -45, 200]:
        heading = (heading + turn) % 360
        print(heading)
  6. [1 mark]Why does setting the same seed before generating numbers give the same sequence each time?

    1. AThe generator is pseudo-random: each number is calculated from the previous state, starting from the seed
    2. BThe computer stores every random number it has ever made
    3. CThe seed stops the numbers being random at all times
    4. DRandom numbers always repeat after one use

The task: drive a command string

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.

QR code
Do it on the robot
www.bugbotlab.com/learn/a1-2-operations-and-strings/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Do the task without split, finding each comma with find and taking substrings between them.
  2. What do -7 // 2, -7 % 2 and int(-7 / 2) give? Explain why two of them differ.
  3. Add a command W that waits a number of tenths of a second, so W15 waits 1.5 seconds.