Talking to each other · Robot club · about 20 min
Agreeing what a message looks like; splitting it into words and numbers.
[1 mark]What does this program print?
words = "forward 30".split() command, amount = words[0], float(words[1]) print(command, amount * 2)
[1 mark]What does this program print?
def numbers_in(text):
out = []
for word in text.replace(",", " ").split():
try:
out.append(float(word))
except ValueError:
pass
return out
print(numbers_in("ball at -12,40 ok"))[1 mark]What does this program print?
text = " Forward 30 " words = text.lower().strip().split() print(words)
[1 mark]Why does ask give up after a second instead of waiting for a reply forever?
messages() stops working after a second[1 mark]A message arrives saying jump 10, which your program does not understand. What should it do?
[1 mark]ask("next") returns None. What does that mean?
[1 mark]What is the word for the agreement, made in advance, about what messages look like?
Ask the Guide for instructions one at a time, do each one, and stop at done. You will end up in the green zone.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def ask(question, seconds=1.0):
# send a question and return the first reply that arrives within `seconds`, or None
send(question)
for tick in range(int(seconds * 10)):
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
for sender, text in messages():
return text
return None
def numbers_in(text):
# every number in a message, as floats: "at 25,55" -> [25.0, 55.0]
out = []
for word in text.replace(",", " ").split():
try:
out.append(float(word))
except ValueError:
pass
return out
reply = ask('next')
print('guide says:', reply)Plan your program here, then type it in and press Run.
left <cm> and backward <cm> too, even though this Guide never sends them.turn <degrees>.ask returns None, try again, up to three times, then give up and print why.