Project: survey the room

Look in eight directions, store the survey as records, report on it and head for open space.

F3.8Strings, lists and recordsGCSE25 min

Do this lesson in the simulator

Before a robot can plan a route, it has to know what is around it. In this project BugBot does a survey: it turns on the spot, looks in eight directions, stores every look as a record in a list, and then reports on its data like a scientist, before driving off towards the most open space. It brings together the whole of F3: strings, lists, records and the loop patterns.

The brief

Standing still, the robot looks in 8 directions, 45 degrees apart. For each direction it stores a record with the direction in degrees and the distance ahead. After the survey it prints a table of the records, then a report: the nearest and farthest directions, the average distance, and how many directions are open (more than 40 cm of room). Finally it turns to face the farthest direction and plays a note.

Plan the data first

Decide what one record looks like before writing any loops:

{"direction": 45, "distance": 42.0}

The survey is a list of 8 of these. Every question in the report is then a loop over that list.

Report line Pattern
nearest direction smallest so far, keeping the record
farthest direction largest so far, keeping the record
average distance running total, divided by the number of records
open directions count the records with distance over 40

Step 1: collect the survey

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

survey = []
for i in range(8):
    survey.append({"direction": i * 45, "distance": distance()})
    turn_right(30, angle=45)

for r in survey:
    print(r)

Run this in the simulator

Check the printout: eight records, directions 0 to 315. The robot turned all the way round, so it faces the way it started.

Step 2: a table

Print one line per record, with the numbers lined up. An f-string can set a width: {r['distance']:6} uses at least six characters.

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

survey = [{"direction": 0, "distance": 31.0}, {"direction": 45, "distance": 42.5}, {"direction": 90, "distance": 51.0}]
print("direction  distance")
for r in survey:
    print(f"{r['direction']:9}  {r['distance']:8}")

Run this in the simulator

This cell uses a short made-up survey, so you can get the table right without waiting for the robot to turn.

Step 3: the report

Write each report line as its own small loop over survey, testing each on the made-up survey first. Then put steps 1 to 3 together in the task, and finish with the turn and the note.

Test with data you can check

Before trusting the report, work one out by hand. For the made-up survey above: the nearest is direction 0 at 31.0 cm, the farthest is direction 90 at 51.0 cm, the average is 41.5 cm, and two directions are open. If your program says something else, trace it.

Task: survey the room

Do the survey from the brief. Print the table, then exactly these report lines (your numbers will differ):

nearest: 180 degrees, 15.0 cm
farthest: 90 degrees, 51.0 cm
average: 32.4 cm
open directions: 3

Then turn to face the farthest direction and play a note.

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

survey = []
for i in range(8):
    turn_right(30, angle=45)

Challenges

  1. Add a field open to each record, True or False, and use it for the count.
  2. Round the average to one decimal place, and print the table with a line of dashes under the heading.
  3. Survey in 16 directions instead of 8. What must change, and what can stay the same?