The answersDownload the PDF
Worksheet

0.4 Deciding

Python quick start · Robot club · about 12 min

BugBotLab
NameClassDate

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")
  2. [1 mark]What does this program print?

    gap = 30
    if gap > 30:
        print("go")
    else:
        print("stop")
  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:
  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
  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
  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
  7. [1 mark]Which two symbols together mean "is not" in Python?

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())

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/start-4-if/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Make the light red when the gap is under 20 cm and green when it is over.
  2. Stop at 15 cm, back off 10 cm, and stop again.
  3. Slow down as you get closer: speed 60 while the gap is over 40, speed 25 after that.