Project: ask and drive
Plan, build and test one program that asks, drives a square, plays notes and reports.
Do this lesson in the simulatorTime to put the module together. You will build one program, a piece at a time, that asks the user two questions, drives a square from the answers, plays a note at every corner and reports on what it did. Every idea from F1 is in it: sequence, output, calling functions, variables and constants, input, casting and arithmetic.
The brief
The robot asks how long each side of a square should be, in centimetres, and which note to play, in hertz. It drives the square, playing the note for a fifth of a second at each corner, and ends where it started. Then it prints the perimeter and the area of the square it drove.
Plan before you type
Programmers plan a program by its inputs, what it does with them, and its outputs. Fill this in on paper first:
| Inputs | Processing | Outputs |
|---|---|---|
| side length (a real number) | four drives, a note at each corner | the robot's drive and four notes |
| note (a whole number) | perimeter = 4 x side | perimeter: ... |
| area = side x side | area: ... |
Which data type should each input be? The side could be 12.5 cm, so a float. A note is a whole number of hertz, so an integer.
Step 1: ask and convert
Start with the questions, and print the answers to check them. Checking each step before writing the next is how you catch mistakes while they are small.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
side = float(input("How long is each side, in cm? "))
note = int(input("Which note, in hertz? "))
print("side", side, "note", note)
Try 20 and 440. Then try 20.5 and 440: the float copes. What happens with 440.5 for the note, and why?
Step 2: one side and one corner
Add one drive and one note, and run it:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
SPEED = 50
side = float(input("How long is each side, in cm? "))
note = int(input("Which note, in hertz? "))
forward(SPEED, distance=side)
tone(note, 0.2)
SPEED is a constant: every drive will use it, so it lives in one place at the top.
Step 3: the whole square
BugBot can drive in any direction without turning, so a square is forward, right, backward, left. Copy step 2 into the task below and build the other three sides and corners. When the robot draws a square and ends where it started, add the report.
Step 4: the report
Work the perimeter and area out from side with operators. Do not type the answers in: the program has to be right for any side the user gives.
Testing
Run your program with several answers and check each one against what you worked out by hand:
| Side | Note | Perimeter should be | Area should be |
|---|---|---|---|
| 25 | 523 | 100.0 | 625.0 |
| 10 | 880 | 40.0 | 100.0 |
| 12.5 | 262 | 50.0 | 156.25 |
The task types in 25 and 523, the first row.
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
Challenges
- Ask for the speed as well, as a third input.
- Make the robot play a different note at each corner: the note, then a note twice as high, then back.
- Drive a rectangle instead: ask for the width and the length, and report its perimeter and area.