Bug Hunters
Syntax, runtime and logic errors
Your mission
Three programs, three very different bugs. Run each one to see how its error behaves, track down the line that causes it, fix the bugs yourself, break a working program on purpose, then hunt the bugs in a stream of new programs.
Explore
Run each program, compare what happens with the expected output, then tap the line that causes the problem.
Program 1: Say hello
Expected output: Hi there, then Hello
Program 2: Divide
Expected output: Dividing..., then a number
Program 3: Add two numbers
Expected output: 12
Three programs, three bugs, and every bug behaves differently. Run each program and compare what happens with the expected output. Then tap the line that causes the problem.
0 of 6 discoveries made
Experiment
Fix the three bugs, then break a working program in all three ways.
Tap a dashed piece of code to change it, then press Run.
Program 1
Program 2
Program 3
This program works: it should print 65. Tap the dashed pieces to change them, and make it fail in all three ways.
Bug-hunting experiments
Try each one. They tick themselves off.
0 of 6 experiments done
Challenge
Six new programs: name the kind of error, and tap the line that causes it.
Bug hunter's field guide
- Syntax error: invalid code; the program cannot run at all
- Runtime error: starts, then crashes during execution
- Logic error: runs, but the result is wrong
- Always compare the output with the expected output
Catch all 6 bugs to finish this stage.
Understand
Here is what the bugs were showing you, in the words you need for the exam.
Three kinds of programming error
| Error type | What happens | Example | How you notice |
|---|---|---|---|
| Syntax error | The program contains invalid code syntax, so it cannot run until the syntax is fixed. | Missing a closing parenthesis in print("Hello" | An error message before anything runs |
| Runtime error | Something goes wrong during execution, and the program stops. | Dividing a number by zero | The program starts, then crashes with an error message |
| Logic error | The program runs, but gives an incorrect result. | Using subtraction when the intended operation was addition | No error message: only by comparing the output with the expected output |
Key takeaway: simply running a program does not guarantee that the result is correct. Different errors affect programs in different ways: some stop the program, and some produce wrong results.
Syntax errors
Every language has rules about how code must be written, like grammar. Break one, such as a missing bracket, quote or colon, and the code is not valid. Python checks the whole file before running it, so in Program 1 not even the first line ran.
Runtime errors
The code is valid, so the program starts. Then an instruction cannot be carried out, such as number / 0, and the program stops with an error such as ZeroDivisionError: division by zero. Lines before the problem have already run.
Logic errors
The hardest to spot: there is no error message. The program does exactly what it was told, but what it was told is wrong: a - b instead of a + b, > 28 instead of >= 28, or range(1, 10) when you wanted 1 to 10. That is why programmers test their programs with inputs whose correct output they already know.
Words to know
- Bug
- A mistake in a program that makes it fail or give wrong results.
- Debugging
- Finding and fixing the bugs in a program.
- Syntax
- The rules for how code in a language must be written.
- Syntax error
- Invalid code syntax: the program cannot run until it is fixed.
- Runtime error
- Something goes wrong during execution, and the program stops.
- Logic error
- The program runs but gives an incorrect result.
Exam tip
To classify an error, ask two questions. Did the program run at all? No: syntax error. Did it stop with an error message while running? Yes: runtime error. It ran to the end but the answer is wrong: logic error. Then name the cause, for example "missing closing parenthesis", "division by zero" or "subtraction used instead of addition".