AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Test it properly

Week 12 · 1.8 Testing · 3 lessons
3

Syntax or logic?

Computing 9: Topic 1.8, pp. 135–154
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 errorLogic error
What is wrongthe rules of the languagethe idea of the program
Does the program run?noyes
Is there a message?yesno
How is it found?read the messagetrace and test
Worked examples
Python
1mark = int(input("Mark? "))
2if mark >= 50
3    print("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.
Python
1base = int(input("Base? "))
2height = int(input("Height? "))
3area = base * height
4print("Area:", area)
Output
Area: 30
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.
TestLengthWidthHeightExpectedActualPass / fail
14322414FAIL
221244pass
35211011FAIL
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.
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"
3    print("Hello Ali")
4elseif name == "Mia":
5    print("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.
Python
1quantity = int(input("Pencils? "))
2cost = 2 * (quantity + 3)
3print("Cost:", cost)
4
Explain how you would find a logic error in a program that runs without any message but gives a wrong answer.