6.1.5 – Find and fix program errors

6.1.5 – Find and fix program errors

Identify syntax, runtime and logic faults from evidence, trace a failure to its cause, and repair a complete program without losing correct behavior.

Classify the clue

Debugging starts with a requirement. Without one, an output can look plausible even when it is wrong. For example, this program follows Python's rules and meets the requirement display Teen ticket when age is at least 13:

Python
age = 15
if age >= 13:
    print("Teen ticket")

A static reading predicts the output Teen ticket. The grammar is valid, every operation can be completed, and the condition matches the requirement. Those three checks separate the main error types.

Syntax error

A syntax error breaks the grammar rules of the programming language, so Python cannot parse the program as valid code for execution.

If parsing succeeds, Python can begin following the instructions. A failure at that stage belongs to a different category.

Runtime error

A runtime error occurs after a syntactically valid program starts executing, when an operation cannot be completed. An unhandled Python exception then stops execution.

Even executable instructions can express the wrong rule. Correctness must therefore be checked against the stated requirement.

Logic error

A logic error occurs when a program can be parsed and its instructions can execute, but they do not implement the requirement, so the program's behaviour, output or state is wrong.

Error typeDecisive clueCommon near-miss
SyntaxPython cannot parse the source and reports a SyntaxError.A spelling mistake is not a complete definition. The decisive feature is that a language rule is broken.
RuntimeExecution reaches an operation it cannot complete, often producing a traceback and an exception name.Runtime does not mean that the program merely takes a long time.
LogicPython can execute the instructions, but the behaviour disagrees with the requirement.No error message does not prove that the result is correct.

Classification depends on what the source means to Python.

Python
pritn("Hello")

This misspelled call follows the grammar of a function call, but pritn is an unknown identifier. Python can parse it and would then raise NameError during execution, so that particular misspelling causes a runtime error rather than a syntax error.

Repair a syntax error

The requirement is: if at least one space is available, display Ready.

Python
spaces = 3
if spaces > 0
    print("Ready")

Before considering the intended output, inspect whether Python can parse every statement. Line 2 starts an if statement but has no colon after its condition. Python would report a SyntaxError, usually pointing at the place where it expected the missing punctuation. The indicated position is evidence: for other syntax faults, the actual missing token can be just before the pointer.

The smallest correction is to add the colon. There is no reason to rename variables or rewrite the selection.

Python
spaces = 3
if spaces > 0:
    print("Ready")

The corrected output is:

Output
Ready

For a syntax error, inspect the reported line and the code immediately before the pointer, then change only the token or structure that breaks Python's grammar.

Repair the next fragment without running it; source belongs in the code editor.

Trace a runtime failure

A runtime fault is reached during execution, so follow state in execution order. This program is meant to display the total of a subtotal and a delivery charge:

Python
subtotal = 18
delivery = 3
print(subtotl + delivery)

All three lines fit Python's grammar. Line 1 creates subtotal with value 18, and line 2 creates delivery with value 3. On line 3, however, Python is asked for the value of subtotl, which has never been defined. Execution would stop there with NameError. Python's error report is called a traceback; its final line names the exception, while the preceding context helps locate the failing operation.

The failing operation and the cause are both visible on line 3 in this example. The smallest correction is the identifier:

Python
subtotal = 18
delivery = 3
print(subtotal + delivery)

The corrected program's predicted output is 21. This shows why a spelling mistake does not automatically mean syntax error: the misspelled name was still grammatically valid Python.

In a longer program, the traceback line is where the failure becomes visible, not necessarily where the bad value was created. Work backwards through the identifiers used by the failing expression.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account