Exam preparation · GCSE · about 15 min
The ten commonest mistakes, checking on paper, and repairing a broken program.
[1 mark]Which is the commonest way to lose marks you knew?
[1 mark]What is wrong with setting a total to 0 inside a loop?
[1 mark]if x = 5: will not run. Why?
[1 mark]Which three values should you trace a program with?
[1 mark]What does this program print?
total = 0
for n in range(1, 6):
total = total + n
print(total)15
1 + 2 + 3 + 4 + 5 = 15.
This program should drive 4 steps of 15 cm, printing step <n>: <total> cm after each one, then total: 60 cm and turn the LED green. It has three faults. Find them, fix them, and leave the rest alone.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for step in range(1, 4):
total = 0
forward(50, distance=15)
total = total + 15
print(f"step {step}: {total} cm")
print(f"total: {total} cm")
led(255, 0, 0)The hint students can ask for: Three faults: how many times the loop runs, a variable that is set back to zero every time round when it should be set once before the loop, and the colour at the end.
from bugbot import *
connect()
total = 0
for step in range(1, 5):
forward(50, distance=15)
total = total + 15
print(f"step {step}: {total} cm")
print(f"total: {total} cm")
led(0, 255, 0)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.