The worksheetDownload the PDF
Answers

0.3 Doing it again

Python quick start · Robot club · about 12 min

BugBotLab

What this lesson is about

for and while, indentation, and a square drawn with three lines instead of eight.

Questions 7 marks in all

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

    for i in range(4):
        print("this is time number", i)
    Answer:
    this is time number 0
    this is time number 1
    this is time number 2
    this is time number 3

    range(4) counts 0, 1, 2, 3. That is four numbers, but it starts at 0, so the last one is 3.

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

    for i in range(3):
        print("side")
    print("done")
    Answer:
    side
    side
    side
    done

    Only the indented line is inside the loop, so it runs three times. print("done") is not indented, so it runs once, after the loop.

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

    for i in range(3):
        print(i * 10)
    Answer:
    0
    10
    20

    Each time round, i is the next number from range(3): 0, then 1, then 2. Times 10 gives 0, 10 and 20.

  4. [1 mark]What does this program make the robot do?

    for i in range(4):
        forward(50, distance=25)
        turn_right(35, angle=90)
    stop()
    1. ADrive a square, turning clockwise, and finish near where it started
    2. BDrive one straight line 100 cm long
    3. CDrive a triangle
    4. DDrive a square, turning anticlockwise
    Answer: A. Four times: one 25 cm side, then a quarter turn to the right. Four sides and four right turns bring it back near the start.
  5. [1 mark]You want the robot to drive a triangle with equal sides. How many degrees should turn_right turn at each corner?

    Answer: 120. The robot turns all the way round, 360 degrees, over the three corners, so each corner is 360 divided by 3, which is 120.
  6. [1 mark]The robot starts at heading 0 and runs turn_right(35, angle=90) three times. Ignoring small errors, what does heading() say? Give a whole number of degrees.

    Answer: 270 (accept within 2). Each quarter turn to the right adds 90 degrees, so three of them make 270.
  7. [1 mark]Why does a while loop that drives need a small wait() inside it?

    1. ASo time passes and the robot moves between looks; without it the loop spins as fast as it can
    2. BTo make the robot drive more slowly
    3. CPython gives an error for any loop without a wait
    4. DSo that clock() can start counting
    Answer: A. wait() is where time passes and the robot actually moves. Without it the loop spins as fast as the computer can go, no time passes, and BugBot stops the program and asks for a wait(0.1).

The task: four corners

Drive a square: 30 cm a side, turning right at each corner, using a for loop. The four green squares are the corners it has to pass through.

from bugbot import *
connect()

for i in range(4):
    # one side and one corner
    forward(50, distance=30)

stop()

The hint students can ask for: One side and one corner, four times over. A quarter turn is 90 degrees.

A solution

from bugbot import *
connect()
for i in range(4):
    forward(50, distance=30)
    turn_right(35, angle=90)
stop()

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.