Talking to each other · Robot club · about 15 min
messages(): what arrived since you last asked, and who sent it.
[1 mark]What does messages() give you?
sender, text.[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)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.
[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?
[1 mark]A polling loop checks messages() but has no wait. What is the problem?
wait(0.1) still hears each message within a tenth of a second.[1 mark]Your program is busy driving for 5 seconds and does not call messages(). What happens to the Beacon's messages?
[1 mark]You call send("hello") and then messages(). Is your own hello in the list?
send goes to every other robot, so you only get what others sentmessages() does not.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.
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.