GoalTell syntax errors from logic errors, correct them, and use a test table to find a logic error.
New words
syntax error · a mistake in the rules of the language, such as a missing colon, which stops the program from runninglogic error · a mistake that lets the program run but makes it give the wrong resultsyntax · the rules for how the words and symbols of a programming language must be written
Explanation
A syntax error means the code breaks the rules of the language, for example a missing colon or bracket, or a wrong word such as elseif in Python. Python reads the whole program before it runs, so it refuses to start and reports the error. A logic error is different. The program runs and may print something, but it does not do what the requirements say. Examples are using + instead of ×, or < instead of <=. Python gives no message for a logic error, so you must find it yourself. Read the requirements, work out the right answer for some test data, trace the program, and look for the first line where the values go wrong. A test plan helps, because a failed test points to the data that shows the fault. A faulty program can still pass a test by luck, so test with a mix of data.
Syntax error
Logic error
What is wrong
the rules of the language
the idea of the program
Does the program run?
no
yes
Is there a message?
yes
no
How is it found?
read the message
trace and test
Worked examples
Python
1mark = int(input("Mark? "))
2if mark >= 503print("Pass")
Output
SyntaxError: expected ':'
A syntax error. The colon is missing at the end of the if line. Python stops before it runs anything and shows the message. Even if there were a print line above this one, nothing would be printed.
A logic error. The program should work out the area of a triangle, which is half of base × height. The user types 6 and 5. The program runs and prints 30, but the correct answer is 15. The line area = base * height forgets to divide by 2.
Test
Length
Width
Height
Expected
Actual
Pass / fail
1
4
3
2
24
14
FAIL
2
2
1
2
4
4
pass
3
5
2
1
10
11
FAIL
A test table finds a logic error. A box has volume length × width × height, but the faulty program works out length × width + height. Test 1 and test 3 fail. Test 2 passes by luck, because 2 × 1 × 2 and 2 × 1 + 2 are both 4. This is why one passing test does not prove that a program is correct.
Class activity
Error detectives (no computer). Copy three short programs onto paper, taken from this lesson or invented by you, and put one error in each, some syntax and some logic. Give them to a partner. They must say whether each error is syntax or logic, name the line, and correct it. For each logic error they must also give test data that shows the fault. (If you have a computer or tablet, you can type the first program of this lesson and compare Python's message with your own answer.)
Practice
1
Say whether each error is a syntax error or a logic error. (a) print("Hi" with no closing bracket. (b) A program that should add two numbers uses total = a - b. (c) if x > 5 with no colon. (d) elseif used in Python. (e) The rule is ‘20 or less’ but the program uses < 20.
(a) syntax. (b) logic: it runs but gives a wrong answer. (c) syntax. (d) syntax: Python uses elif. (e) logic: the value 20 gives the wrong result.
2
This program has three syntax errors. Find them and write the corrected program. What does the corrected program print when the user types Mia?
Python
1name = input("Name? "2if name == "Ali"3print("Hello Ali")
4elseif name == "Mia":
5print("Hello Mia")
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): name = input("Name? ") / if name == "Ali": / [indent] print("Hello Ali") / elif name == "Mia": / [indent] print("Hello Mia"). Check: The three errors are the missing closing bracket on line 1, the missing colon on line 2 and elseif instead of elif. For Mia it prints Hello Mia.
3
The requirement is: each pencil costs 2 and delivery is a fixed 3. Work out the expected output for the inputs 4 and 0, and the actual output of this program. Which tests fail? Correct the line.
Expected: 4 pencils cost 2 × 4 + 3 = 11, and 0 pencils cost 3. Actual: 2 × (4 + 3) = 14, and 2 × (0 + 3) = 6. Both tests fail. This is a logic error. The corrected line is cost = 2 * quantity + 3.
4
Explain how you would find a logic error in a program that runs without any message but gives a wrong answer.
Read the requirements. Choose test data and work out the expected output by hand. Run the program or trace it in a table and compare. Find the first line where a value is different from what it should be, and look at that line. A failed test with extreme data often points to a wrong comparison.