Python quick start · Robot club · about 12 min
if, else, comparisons, and a robot that looks with distance() before it moves.
[1 mark]What does this program print?
gap = 25
if gap > 30:
print("plenty of room")
else:
print("too close")too close
25 is not more than 30, so the test is false and the else lines run instead.
[1 mark]What does this program print?
gap = 30
if gap > 30:
print("go")
else:
print("stop")stop
30 is not more than 30, it is equal. > is false when the two are the same, so this prints stop. Use >= to include 30.
[1 mark]Which line asks whether gap is 15?
if gap == 15:if gap = 15:if gap is equal 15:if 15:if.[1 mark]What does this program make the robot do?
while distance() > 15:
forward(40)
wait(0.1)
stop()while looks at distance() before every repeat. As soon as the gap is not more than 15, the loop ends and stop() runs.[1 mark]The same program runs with the robot already 10 cm from the wall. What happens?
while distance() > 15:
forward(40)
wait(0.1)
stop()distance() > 15 is false the very first time, so the indented lines are skipped and the program goes straight to stop().[1 mark]gap is 20. Which of these tests are true?
Tick every answer that is true.
gap > 15gap >= 20gap != 20gap < 20gap == 20>= and == are true. It is not less than 20, and != asks whether it is different, which it is not.[1 mark]Which two symbols together mean "is not" in Python?
!= is true when the two sides are different, so gap != 0 means the gap is anything except 0.Drive up to the wall and stop between 10 cm and 20 cm from it, without touching it. Look as you go, rather than guessing a distance.
from bugbot import *
connect()
while distance() > 40:
forward(40)
wait(0.1)
stop()
print("gap", distance())The hint students can ask for: Drive while distance() is more than the gap you want, with a short wait each time round, then stop.
from bugbot import *
connect()
while distance() > 15:
forward(40)
wait(0.1)
stop()
print('gap', distance())
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.