Behaviours · Robot club · about 20 min
From a loop to think(me) and me.memory, with a practice arena.
[1 mark]How often does the arena call your think(me) function?
[1 mark]Why does a brain keep its state in me.memory rather than in a normal variable?
me.memory is fasterme.memory to score you[1 mark]What does this program print?
class Me:
def __init__(self):
self.memory = {}
def think(me):
count = me.memory.get("count", 0)
me.memory["count"] = count + 1
def forgetful(me):
count = 0
count += 1
return count
me = Me()
for i in range(3):
think(me)
n = forgetful(me)
print(me.memory["count"])
print(n)[1 mark]On the very first tick, me.memory is empty. What does me.memory.get("state", "go") give?
"go", the defaultNone[1 mark]What does this program print?
samples = [(20, 0), (25, 45), (60, 90), (80, 135), (70, 180), (30, 225), (20, 270)] opens = [h for d, h in samples if d > 40] target = opens[len(opens) // 2] print(opens, target)
[1 mark]The escape program aims at the middle of the open headings, not the single longest reading. Why?
Three walls around you. Find the gap with the distance sensor, drive out and into the green zone, touching nothing.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# as long as the thing ahead is further than 15 cm
while distance() > 15:
# drive forward at 50 (keeps going until the next command)
forward(50)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
print("wall at", distance())Plan your program here, then type it in and press Run.
think(me) with me.memory) on paper. What changes?