Errors: syntax, runtime and logic
Reading an error message, and the three kinds of error by their exam names.
Do this lesson in the simulatorEvery programmer, on every day of their life, makes mistakes and reads error messages. Getting comfortable with that early is the most useful thing in this module. An error message is not the computer telling you off. It is the computer telling you where to look.
There are three kinds of mistake, and they feel very different: one stops the program before it starts, one stops it half way, and one never stops it at all.
Read the whole message
Run this. It has a mistake on purpose.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
print("starting")
prin("hello")
print("finished")
Two things to notice. "starting" was printed, then the error appeared, and "finished" was not printed. A program stops at the first error it hits.
Now read the red text: NameError: name 'prin' is not defined. It tells you the kind of problem (a name Python does not know), what it was (prin), and where it happened. Fix the spelling and run again.
Syntax errors: Python cannot read it
The syntax of a language is its rules of grammar: brackets that close, quotes that end, colons in the right places. Break a rule and Python cannot understand the program at all, so none of it runs, not even the lines before the mistake:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
print("this line is fine")
print("missing bracket"
SyntaxError. Also try print("missing quote). Syntax errors often point at the end of the line or the line after the real mistake, because that is where Python finally gave up.
Runtime errors: it crashes while running
A runtime error happens when a line that follows the rules cannot be carried out. The program starts, runs up to that line, then crashes:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
led("green")
print("waiting...")
wait("two")
led("off")
The LED went green and "waiting..." was printed, so the program did run. Then wait was given the text "two" when it needs a number of seconds, and the program stopped with a TypeError. The LED never went off.
NameError, TypeError, ValueError and ZeroDivisionError are all runtime errors: Python only finds them when it reaches the line.
Logic errors: no message at all
The hardest kind. The program runs to the end with no error, and does the wrong thing. This program should drive the robot 30 cm up the mat, away from you:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
print("driving up the mat")
backward(50, distance=30)
print("arrived")
No red text, and it even printed "arrived". But the robot went the wrong way. Python did exactly what the program said; the program said the wrong thing. The only way to find a logic error is to test: know what should happen, watch what does, and compare. Watching the robot is part of debugging.
How to fix things
- Read the error. Find the kind and the line number.
- Look at that line. If it looks fine, look at the line above it.
- Change one thing. Run again.
- No error but the wrong result? Say out loud what each line should do, and watch for the first one that does something else.
- Do not change three things at once; you will not know which one mattered.
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")
Challenges
- Write a program with a runtime error on its fifth line and nothing wrong before it. Check that the lines before it still run.
- Write a program with a logic error that makes the robot drive too far. Swap with a partner and find each other's.
- Try
print(10 / 0). Which of the three kinds of error is it?