Algorithms
A syntax error is a mistake in the way the code is written: the code breaks the rules, or grammar, of the programming language. Every language has fixed rules about how statements must be formed, and any statement that does not follow them is a syntax error, however obvious its meaning is to a human reader.
Before a program can run it must be translated into a form the computer can execute, by a compiler or an interpreter, and a translator cannot make sense of code that breaks the rules. A program containing a syntax error therefore cannot be translated, so it cannot run at all until the error is corrected. The translator checks the code before it runs and reports the first syntax error it finds, usually giving a line number, so these faults are discovered and fixed before any testing takes place.
if condition.whille where while is needed.if statement or a loop.x instead of *.score = int(input())
if score >= 50
print("Well done")Output:
SyntaxError: expected ':' (line 2)
The if line has no colon at the end. You can tell what the programmer intended, but the statement does not follow the grammar of Python, so the translator reports the fault and nothing is run.
distance = 12
time = 3
speed = distance / (time * 60
print(speed)Output:
SyntaxError: '(' was never closed (line 3)
Here the bracket opened before time * 60 is never closed, so the arithmetic cannot be worked out. Python reports this syntax error and stops translating, which means the program never reaches the point where it would run.
A runtime error happens while you are running a program. The code has been accepted, so the syntax is fine, and the logic may be correct, but the computer is asked to carry out an operation that cannot be completed. At that moment the program crashes or stops, because the instruction cannot be executed. The important idea is that the operation cannot be completed, not just that an error message appears.
This is different from a syntax error, which stops the program before it runs, and from a logic error, which lets the program finish but gives the wrong result. A runtime error is not simply a program that runs for a long time or repeats forever: a loop that never ends is usually a logic error, because nothing impossible has happened.
Some instructions work for many values but fail for a particular one. Common causes include:
15 / 0 has no answer, so Python raises ZeroDivisionError.IndexError.FileNotFoundError.str() raises TypeError.NameError.A runtime error can appear only for some inputs or conditions, so a program may pass several tests and still crash later. In the program below, if you choose 0, 1, 2 or 3, the program prints a score. If you choose 4, it asks for an item that does not exist, so the program stops with an IndexError.
scores = [12, 8, 15, 6]
position = int(input("Which score? "))
print("Score: " + str(scores[position]))Output when the user types 4:
Which score? 4
IndexError: list index out of range (line 3)A logic error is a mistake in the thinking behind a program rather than in the way it is written. The code is syntactically correct, so the computer accepts every line and runs the program right through to the end — but the result is not the one you wanted. The computer has done exactly what it was told to do; the fault is in what you told it to do.
Because the computer simply follows your instructions, no error message usually appears. The program finishes quietly with the wrong answer, the wrong message, or the wrong value stored in a variable.
> where >= is needed, or + instead of -.count when it should have gone into total.Conditions are where logic errors hide most often, because they control both selection and loops. A loop condition that can never become true makes the loop repeat forever, which is a logic error too.
No compiler can find a logic error for you: every line is valid, so there is nothing for it to report. That is the difference from a syntax error, which breaks the grammar of the language and stops the program running at all.
In this program, anyone aged 12 or under should pay the child price of £6, and everyone else should pay £12.
age = int(input())
price = 12
if age >= 13: # logic error: the condition is back to front
price = 6
print("Ticket price: " + str(price))Output when the user types 12:
12
Ticket price: 12
The rule is written back to front, so adults get the child price while children pay full price. Changing the condition to age < 13 puts it right.
Testing a program means running it with inputs you choose in advance and comparing its actual output with the expected output you worked out by hand. You calculate what should come out in advance, then run the program and compare the two. A mismatch means there is a fault to find, and comparing actual with expected is the first stage of debugging.
The rule for this ride is that you must be at least 140 cm tall.
# checks whether someone is tall enough for the ride
height = int(input("Height in cm: "))
if height > 140:
print("Allowed on")
else:
print("Too short")Output when the user types 140:
Height in cm: 140
Too short
Entering 165 gives the answer you expected, so that test passes. Entering 140, the lowest allowed height, does not, so a fault is revealed: the condition should accept 140 itself, using >=. The passing test on its own told you nothing about it.
Vary the inputs on purpose rather than picking them at random. For a program that checks a username, which must be between 6 and 12 characters long and contain letters and digits only:
| Type of test data | Example input | Why it is used |
|---|---|---|
| Normal data | playerone (9 characters) | An ordinary length the program should accept. |
| Boundary data | abc123 (6 characters) and longusername (12 characters) | The shortest and longest allowed lengths, where length checks are most likely to be wrong. |
| Invalid data | abc12 (5 characters), thisiswaytoolong (16 characters) or bad!name | Values the program should reject: too short, too long, or a symbol that is not allowed. |
A runtime error often appears for just one input: the program works again and again, then stops on the value that asks for something impossible. A logic error can give the right answer for one value and the wrong answer for another, as the ride program does. One successful test proves only that the program works for that one input.
Testing can never reveal a syntax error. A program that breaks the rules of the language will not run at all, so there is nothing to test: the compiler or interpreter reports the mistakes before it starts. Those are found and fixed first, and testing only ever begins on a program that already runs.