Decisions and loops · GCSE · about 25 min
A while loop, decisions and the buzzer: beep faster as the wall gets closer, and park.
[1 mark]Why does the parking sensor use a while loop rather than a for loop?
[1 mark]What does this program print?
d = 35
if d > 50:
gap = 0.4
elif d > 30:
gap = 0.25
else:
gap = 0.1
print(gap)[1 mark]The robot stops slightly closer to the wall than BAY. Why?
[1 mark]Where must beeps = 0 go?
Drive to the wall, beeping faster as it gets closer, and stop in the green bay without touching the wall. Then play a long note, turn the LED red and print one line in exactly this shape (your numbers will differ):
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
BAY = 20
beeps = 0
while distance() > BAY:
forward(30)
wait(0.1)
stop()Plan your program here, then type it in and press Run.