The worksheetDownload the PDF
Answers

9.2 Listening

Talking to each other · Robot club · about 15 min

BugBotLab

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
    Answer: A. Each call empties the queue, and each item is a pair you can unpack as sender, text.
  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)
    Answer:
    heard Beacon: tick 1
    heard Beacon: tick 2

    Unpacking each pair gives the sender and the text. The Scout's message is skipped because it is not from the Beacon.

  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?

    Answer: 4. Ticks at 0.1, 1.1, 2.1 and 3.1 seconds have arrived. The radio kept them until you asked.
  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
    Answer: A. Check, act, wait. A short wait(0.1) still hears each message within a tenth of a second.
  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
    Answer: A. The radio does not lose what you do not read straight away.
  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
    Answer: A. Reading your own message as if someone else said it is a classic bug. The radio log shows yours; messages() does not.

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())

The hint students can ask for: The Beacon sends tick <n> every second. Keep asking messages() and print heard <sender>: <text> for each one. Do not drive.

A solution

from bugbot import *
connect()
for tick in range(70):
    for sender, text in messages():
        print(f"heard {sender}: {text}")
    wait(0.1)

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