The worksheetDownload the PDF
Answers

F1.4 Errors: syntax, runtime and logic

Programming basics · GCSE · OCR J277 2.3.2, AQA 8525 3.2.11, Edexcel 1CP2 1.2.5 · about 15 min

BugBotLab

What this lesson is about

Reading an error message, and the three kinds of error by their exam names.

Questions 6 marks in all

  1. [1 mark]print("hello" is missing its closing bracket. What kind of error is this?

    1. ASyntax error
    2. BRuntime error
    3. CLogic error
    4. DIt is not an error
    Answer: A. The code breaks the rules of the language, so Python cannot run it at all.
  2. [1 mark]A program runs with no error message, but the robot drives backwards when it should go forwards. What kind of error is this?

    1. ALogic error
    2. BSyntax error
    3. CRuntime error
    4. DNo error: the program finished
    Answer: A. The program runs to the end but does the wrong thing. Only testing finds a logic error.
  3. [1 mark]A program prints two lines, then crashes on print(10 / 0). What kind of error is this?

    1. ARuntime error
    2. BSyntax error
    3. CLogic error
    4. DIt prints 0
    Answer: A. The line follows the rules, but it cannot be carried out when the program reaches it.
  4. [1 mark]A program has a syntax error on its last line. How many of its lines run?

    1. ANone
    2. BAll the lines before the error
    3. CAll of them
    4. DOnly the last line
    Answer: A. Python cannot read a program with a syntax error, so it runs none of it. A runtime error is different: the lines before it do run.
  5. [1 mark]Which of these would stop the program with an error message?

    Tick every answer that is true.

    1. Await("two")
    2. Bprin("hi")
    3. Cforward(50, distance=30) when you meant 20
    4. Dstop with no brackets
    Answer: A, B. wait("two") and prin("hi") both fail when they run. Driving 30 instead of 20 and a bare stop give no message: they are logic errors.
  6. [1 mark]Put the steps for fixing an error in order.

    Number the lines 1 to 4 to put them in the right order.

    1. Read the error: find the kind and the line number
    2. Look at that line, and the line above if it looks fine
    3. Run again
    4. Change one thing
    Answer:
    Read the error: find the kind and the line number
    Look at that line, and the line above if it looks fine
    Change one thing
    Run again

    Change one thing at a time, so you know which change mattered.

The task: fix three errors

This program should turn the LED green, wait a second, drive 30 cm forward into the green zone and print arrived. It has one syntax error, one runtime error and one logic error. Fix them one at a time.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

led("green"
wait("1")
backward(50, distance=30)
print("arrived")

The hint students can ask for: One syntax error, one runtime error, one logic error. Fix the first error you are shown, run again, and watch where the robot goes.

A solution

from bugbot import *
connect()
led("green")
wait(1)
forward(50, distance=30)
print("arrived")

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