The worksheetDownload the PDF
Answers

F1.10 Project: ask and drive

Programming basics · GCSE · about 25 min

BugBotLab

What this lesson is about

Plan, build and test one program that asks, drives a square, plays notes and reports.

Questions 4 marks in all

  1. [1 mark]In the project, which of these is an output?

    1. AThe perimeter printed at the end
    2. BThe side length the user types
    3. CThe note the user types
    4. DThe constant SPEED
    Answer: A. The user's answers are inputs. What the program prints, and the drive and notes, are outputs.
  2. [1 mark]What does this program print?

    side = float("12.5")
    print("perimeter:", 4 * side)
    print("area:", side * side)
    Answer:
    perimeter: 50.0
    area: 156.25

    side is 12.5, so the perimeter is 50.0 and the area is 156.25.

  3. [1 mark]Why does the project convert the note with int but the side with float?

    1. AA side can be a fraction of a centimetre; a note is a whole number of hertz
    2. Bint is faster
    3. Ctone() only accepts text
    4. Dfloat cannot be used twice
    Answer: A. Choose the data type that fits the values that make sense.
  4. [1 mark]You test with a side of 10. What should the area be?

    1. A100
    2. B40
    3. C20
    4. D10
    Answer: A. Area is side times side. Working the answer out by hand first is what makes a test useful.

The task: ask and drive

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

SPEED = 50

# ask the two questions and convert the answers

# drive the square, a note at each corner

# print the perimeter and the area

The hint students can ask for: Ask for both answers and convert each to the kind of number it should be: a side can have a decimal, a note cannot. Drive the four sides in order, sounding the note after each one, then work the perimeter and area out from the side you were given.

A solution

from bugbot import *
connect()
SPEED = 50

# ask the two questions and convert the answers
side = float(input("How long is each side, in cm? "))
note = int(input("Which note, in hertz? "))

# drive the square, a note at each corner
forward(SPEED, distance=side)
tone(note, 0.2)
right(SPEED, distance=side)
tone(note, 0.2)
backward(SPEED, distance=side)
tone(note, 0.2)
left(SPEED, distance=side)
tone(note, 0.2)

# print the perimeter and the area
print("perimeter:", 4 * side)
print("area:", side * side)

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.