Robust programs · GCSE · OCR J277 2.3.2, AQA 8525 3.2.11, Edexcel 1CP2 6.1.2 · about 15 min
Finding the mistake that gives no message: watching, printing, stepping and common bugs.
[1 mark]What is the first step in finding a logic error?
[1 mark]A loop meant to run four times uses range(3). What kind of bug is this?
[1 mark]Why change only one thing at a time when debugging?
[1 mark]Which help find where a logic error first appears?
Tick every answer that is true.
[1 mark]What does this program print?
total = 0
for x in [4, 6]:
total = 0
total = total + x
print(total)6
total is reset inside the loop, so it ends as just the last value.
This program should drive a square of 25 cm sides clockwise, turning right at each corner, and finish back at the start, facing the way it began. It has three logic errors. Find them with the trail, prints or Debug, and fix them one at a time.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
side = 25
for i in range(3):
side = 20
forward(60, distance=side)
turn_left(30, angle=90)The hint students can ask for: Three bugs: the loop runs three times, side is reset to 20 inside the loop, and the turn goes the wrong way.
from bugbot import *
connect()
side = 25
for i in range(4):
forward(60, distance=side)
turn_right(30, angle=90)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.