The answersDownload the PDF
Worksheet

9.2 Listening

Talking to each other · Robot club · about 15 min

BugBotLab
NameClassDate

What this lesson is about

messages(): what arrived since you last asked, and who sent it.

Questions 6 marks in all

  1. [1 mark]What does messages() give you?

    1. AEverything that arrived since you last asked, oldest first, as (sender, text) pairs
    2. BOnly the newest message
    3. CEvery message ever sent in the run, including yours
    4. DThe text of the next message, waiting until one comes
  2. [1 mark]What does this program print?

    inbox = [("Beacon", "tick 1"), ("Scout", "at 25,55"), ("Beacon", "tick 2")]
    for sender, text in inbox:
        if sender == "Beacon":
            print("heard", sender + ":", text)
  3. [1 mark]The Beacon sends a message once a second, the first at 0.1 s. Your program waits 3.2 s and then calls messages(). How many messages does it get?

  4. [1 mark]A polling loop checks messages() but has no wait. What is the problem?

    1. AIt asks thousands of times a second and the robot has no time for anything else
    2. BIt misses every message
    3. CIt sends a message each time round
    4. DNothing, it is the fastest way to listen
  5. [1 mark]Your program is busy driving for 5 seconds and does not call messages(). What happens to the Beacon's messages?

    1. AThey queue up and are all there next time you ask
    2. BThey are lost
    3. COnly the last one is kept
    4. DThe Beacon stops sending
  6. [1 mark]You call send("hello") and then messages(). Is your own hello in the list?

    1. ANo: send goes to every other robot, so you only get what others sent
    2. BYes, everyone hears everything, you included
    3. CYes, as the first item
    4. DOnly if nobody else replied

The task: listen

Print heard <sender>: <text> for each of the Beacon's messages, at least five of them. Do not drive.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# pause 2 s (the robot keeps doing what it was told)
wait(2)
print(messages())

Plan your program here, then type it in and press Run.

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

Challenges

  1. Count the ticks and print the total at the end.
  2. Print the time between one tick and the next, using clock().
  3. Stop listening as soon as tick 5 arrives.