Skip to content
BENIBIX Learn

Bugs and Error Types

Level 5 · Errors and debugging

What a bug is, and the three kinds of error: syntax, runtime and logic. Why the off-by-one error is the most common bug of all.

Course contentsLesson 15 of 18

After this lesson you can

  • Tell syntax, runtime and logic errors apart
  • Explain why a logic error is the hardest to find
  • Spot an off-by-one error in a loop

12 mincovers 6 glossary entries

Worth reading first: For and While Loops, Functions in Python

What kinds of error a program can have

A bug is a mistake in a program that makes it do the wrong thing, or stop. The word goes back a long way. In 1947, engineers working on an early computer at Harvard found a moth stuck in a part and taped it into their logbook. Every programmer writes bugs. The skill is finding them.

An error is the problem a bug causes, and errors come in three kinds. They differ in when they show up and how loudly. Knowing which kind you have tells you where to look, so it is worth learning to tell them apart.

A syntax error breaks the grammar of the language: a missing bracket, a missing colon, a quote mark left open. Python checks the whole file first, so a syntax error stops it before a single line runs. Python names the line and points near the spot. These are the easiest to fix.

A runtime error shows up while the program is running. It happens only when a certain line meets a certain value. Dividing by zero causes one. So does reading names[5] from a list of 3, or asking for a key that is not there. The code was fine as text, but the data broke it.

A logic error is the sneakiest. The program runs to the end with no message, but the answer is wrong. Typing + where you meant -, or testing > where you meant >=, gives a logic error. Python cannot know what you meant, so only a test that checks the answer will catch it.

The most common logic error has its own name: the off-by-one error. A loop runs one time too many or one too few. A list of 5 items has spots 0 to 4. A loop that goes up to 5, rather than stopping before it, reads a spot that does not exist.

People expect errors to be bad news. A loud error is the good kind: it tells you the line and stops. A quiet wrong answer can run for months. That is why it pays to check results, not just that the program finished.

The three kinds of error

KindWhen it shows upExample
Syntax errorBefore any line runsA missing colon after if
Runtime errorWhile running, on some dataDividing by zero
Logic errorNever, the answer is just wrongUsing + instead of -
Five items sit at spots 0 to 4. One step too far reads a spot that is not there.

Worked example

Lucy's average is always a bit low

Lucy's script averages 10 test scores, but the answer is always a little low. No error ever appears.

Her loop used range(1, 10), which skips spot 0, so it added 9 scores and divided by 10. It was an off-by-one logic error. She changes it to range(10) and the average is right.

The words for this lesson6

Check yourself

A program runs with no message but prints the wrong total. What kind of error is it?

Finished this lesson?

Marking it complete also marks its 6 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.