Python quick start · Robot club · about 12 min
for and while, indentation, and a square drawn with three lines instead of eight.
[1 mark]What does this program print?
for i in range(4):
print("this is time number", i)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.
[1 mark]What does this program print?
for i in range(3):
print("side")
print("done")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.
[1 mark]What does this program print?
for i in range(3):
print(i * 10)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.
[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 mark]You want the robot to drive a triangle with equal sides. How many degrees should turn_right turn at each corner?
[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.
[1 mark]Why does a while loop that drives need a small wait() inside it?
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).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.
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.