Concepts / How to Read Error Messages

How to Read Error Messages

Syntax errors break Python's grammatical rules and prevent the program from running. Python reports them immediately with a line number and error message.

  • Programming

Three Kinds of Failure

When a program does not work, the mistake is not always the same kind of mistake. A syntax error prevents Python from running the program. A logic error allows the program to run but puts valid statements in the wrong order or relates them incorrectly. A semantic error also allows the program to run, but the calculation, detail, or variable choice does not achieve the intended goal. Identifying the category first makes debugging more efficient.

readinvalid grammarvalid grammarsteps conflict with goalcomputation misses goalSource codeGrammar checkSyntax errorBefore executionLogic errorWrong order or relationshipExecutionSemantic errorWrong calculation or detail
When does Python detect each error type, and does the program reach execution before the error appears?

Syntax Errors Before Execution

A syntax error breaks Python's grammatical rules. Because Python cannot parse the instruction as valid Python, the program does not run. Python reports the problem immediately, including a line number and an error message. This makes syntax errors frustrating, but usually quick to locate: begin with the reported line and inspect the grammar around the marked location.

python

In this example, primt is not a valid Python function name; the intended spelling is print. Python cannot parse the line because it encounters an unknown token. The caret points to where Python first realized that something was wrong. The marked location is useful evidence, but the underlying cause is the invalid name.

points nearlocatesexplainsprimtfunction namereported linewhere to inspect("Hello")argumentcaretfirst detected locationerror messagegrammar feedback
How do the reported line number, location marker, and error message point to the grammatical problem?

Logic Errors During Execution

A logic error occurs when the code is syntactically valid, but statements are in the wrong order or relate to one another incorrectly. Python can execute the instructions because they follow the grammar. The problem is that the sequence of valid instructions defeats the intended goal. The program may produce incomplete or out-of-order output instead of stopping with a syntax error.

python
Output
10

Every statement in this example is valid, so Python runs it. The problem is the order: the total is printed before the additional item is added. To debug a logic error, trace the statements in the order Python executes them and ask whether each step happens before the result is used or displayed.

thenthenthenthentotal = 10total = 10print(total)total = total + 5total = total + 5print(total)
How can valid statements produce an incomplete result when they are arranged in the wrong order?

Semantic Errors in Valid Code

A semantic error occurs when the code is syntactically perfect and logically ordered, but the program still fails to achieve the intended goal. The mistake may be in a calculation, a detail, or the choice of a variable. Python can execute the instructions and produce output, yet the output is wrong because the instructions compute something different from what the programmer meant.

python
Output
11

This code is grammatically valid and the statements are in a sensible order, so Python can run it. However, if the intended goal is to calculate the cost of several items, adding price and quantity is the wrong calculation. A semantic error is found by comparing the result and the operation with the intended goal, not merely by checking whether Python accepted the code.

can detect grammar failurevalid grammarvalid grammarwrong orderwrong meaningSyntaxgrammar validityProgram stopsinvalid grammarLogicstep relationshipsProgram runsvalid instructionsSemanticsintended meaningWrong resultunintended behavior
Why can Python recognize invalid grammar but not always know that valid instructions produce an unintended result?

Python Follows the Written Instructions

Python always executes exactly what you ask, not what you intend. It can check whether code follows Python's grammar, but valid grammar does not reveal the programmer's private goal. That is why a program can run successfully while producing incomplete, out-of-order, or incorrect output. Debugging is the process of matching the program's actual behavior to the intended goal.

A Feedback-to-Cause Trace

  1. First decide whether Python stopped before execution or produced output. A syntax error prevents the program from running; logic and semantic errors allow it to run.
  2. For a syntax error, use the reported line number, error message, and caret as evidence. Inspect the grammar near the marked location.
  3. For a logic error, trace the statements in execution order and check whether related steps happen in the order required by the goal.
  4. For a semantic error, inspect the calculation, variable choice, or detail and compare it with the intended result.
  5. State the intended behavior explicitly, then compare it with what Python actually did.
  6. Correct the underlying cause rather than assuming every failure is the same kind of error.
classifyinspect evidenceexplain mismatchfixPython feedbackExecution phasestopped or ranFaulty locationline or stepRoot causegrammar, order, or meaningCorrectionmatches the goal
How do you move from Python's feedback to the faulty line, the underlying cause, and the correction?

Mistakes Beginners Make

  • Treating every failure as if it were a syntax error.

    Logic and semantic errors can run successfully because their instructions are grammatically valid.

    Fix: First determine whether the program stopped or produced output, then choose a tracing strategy.

  • Replacing the marked location without checking the cause.

    The caret shows where Python first realized something was wrong; the underlying issue may be the token or name near that location.

    Fix: Use the line number, caret, and error message together to inspect the grammar.

  • Checking only whether the program runs.

    A running program can still have a logic error or semantic error.

    Fix: Compare the output and the executed steps with the intended goal.

  • Ignoring variable choice or calculation details.

    These are semantic mistakes: the code can be syntactically perfect and logically ordered while computing the wrong result.

    Fix: Inspect what the code actually computes and compare it with what it should compute.

Practice the Classification

MEDIUM

Classify each situation as a syntax, logic, or semantic error. Then state the first debugging action you would take: inspect Python's grammar feedback, trace statement order, or compare the calculation with the intended goal.

Hints
  • Ask whether Python can parse and start the program.
  • If the program runs, ask whether the steps happen in the right order.
  • If the order is sound, inspect the calculation, variable choice, or other detail.

A Three-Step Diagnosis

A programmer reports three different problems: Python rejects a line containing primt; a total is printed before another item is added; and a calculation uses the wrong variable.

Classify the rejected line: The invalid name breaks Python's grammatical parsing, so this is a syntax error. Start with the reported line, caret, and error message.

Classify the premature total: The statements are valid, but the total is displayed before the later addition. This is a logic error. Trace the execution order.

Classify the wrong variable: The code can be syntactically correct and logically ordered while using a variable that does not serve the intended calculation. This is a semantic error. Compare the actual computation with the goal.

The three situations require three different debugging strategies: grammar feedback for syntax, execution-order tracing for logic, and comparison with the intended calculation for semantics.

Key Takeaways

  1. Syntax errors break Python's grammar, prevent execution, and are reported with feedback such as a line number and error message.
  2. Logic errors run because the code is valid, but the statements are ordered or related in a way that defeats the goal.
  3. Semantic errors run because the code is valid and ordered, but a calculation, variable choice, or detail produces the wrong result.
  4. Python executes exactly what is written, so debugging requires comparing actual behavior with intended behavior.
  5. Classifying the error first tells you whether to inspect grammar, trace execution order, or examine meaning and calculation.

Key Takeaways

  • Syntax errors stop Python before execution because the code breaks grammatical rules.
  • Logic errors allow execution but produce incorrect behavior because valid steps are ordered or related incorrectly.
  • Semantic errors allow execution but produce a wrong result because the calculation, variable, or detail does not match the intended goal.
  • Python follows written instructions rather than intentions, so debugging compares actual behavior with the goal.
  • Use Python's feedback for syntax errors, execution tracing for logic errors, and goal-to-calculation comparison for semantic errors.