The worksheetDownload the PDF
Answers

0.4 Deciding

Python quick start · Robot club · about 12 min

BugBotLab

What this lesson is about

if, else, comparisons, and a robot that looks with distance() before it moves.

Questions 7 marks in all

  1. [1 mark]What does this program print?

    gap = 25
    if gap > 30:
        print("plenty of room")
    else:
        print("too close")
    Answer:
    too close

    25 is not more than 30, so the test is false and the else lines run instead.

  2. [1 mark]What does this program print?

    gap = 30
    if gap > 30:
        print("go")
    else:
        print("stop")
    Answer:
    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.

  3. [1 mark]Which line asks whether gap is 15?

    1. Aif gap == 15:
    2. Bif gap = 15:
    3. Cif gap is equal 15:
    4. Dif 15:
    Answer: A. Two equals signs ask a question. One equals sign gives a name a value, and Python will not accept it in an if.
  4. [1 mark]What does this program make the robot do?

    while distance() > 15:
        forward(40)
        wait(0.1)
    stop()
    1. ADrive forward, looking as it goes, and stop once the gap is 15 cm or less
    2. BDrive exactly 15 cm and stop
    3. CDrive backwards away from the wall
    4. DDrive until it touches the wall
    Answer: A. The while looks at distance() before every repeat. As soon as the gap is not more than 15, the loop ends and stop() runs.
  5. [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()
    1. AThe loop never runs, so the robot stops straight away without moving
    2. BIt drives into the wall
    3. CIt backs up to 15 cm
    4. DPython gives an error
    Answer: A. The test distance() > 15 is false the very first time, so the indented lines are skipped and the program goes straight to stop().
  6. [1 mark]gap is 20. Which of these tests are true?

    Tick every answer that is true.

    1. Agap > 15
    2. Bgap >= 20
    3. Cgap != 20
    4. Dgap < 20
    5. Egap == 20
    Answer: A, B, E. 20 is more than 15, and it is equal to 20, so >= and == are true. It is not less than 20, and != asks whether it is different, which it is not.
  7. [1 mark]Which two symbols together mean "is not" in Python?

    Answer: !=. != is true when the two sides are different, so gap != 0 means the gap is anything except 0.

The task: stop at the wall

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.

A solution

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.