Concepts / Debugging Strategies for Finding Errors in Your Code

Debugging Strategies for Finding Errors in Your Code

Semantic errors occur when code runs without error messages but produces incorrect results—the hardest kind of error to catch.

  • Programming

When Running Code Is Not Enough

A program can run from beginning to end and still be wrong. This happens when the code follows Python's grammar but does not express the meaning you intended. Such a mistake is called a semantic error. Unlike an error that stops execution, a semantic error may produce ordinary-looking output, so you must compare that output with your expectation.

causescausesSyntax errorGrammar is invalidProgram does not runPython refuses to executeitSemantic errorMeaning is incorrectUnexpected outputCode runs without a warning
What is the difference between code that cannot run because its syntax is invalid and code that runs but does the wrong thing?

The Comma Trap

What do you think happens?

What do you think this statement prints?

  • 1000000
  • 1,000,000
  • 1 0 0
Reveal answer

Answer: 1 0 0

In Python, the commas separate the values 1, 0, and 0. The print function receives three arguments and places a space between them.

python
Output
1 0 0

The commas are not formatting marks for one large integer. They separate three integer values: 1, 0, and 0. Those values become three arguments to print(), and print() displays multiple arguments with spaces between them. The statement is syntactically valid, so Python runs it. Its meaning is nevertheless different from the intended meaning of one million.

separatesseparatesseparatesargumentargumentargumentdisplays1,000,000Comma-separated values1Integerprint()Three arguments1 0 0Spaces between arguments0Integer0Integer
How can valid code produce unexpected output, and which statement creates the incorrect value?

Correct Numeric Literals

To represent one million as one integer, write the digits without commas or spaces. Python also allows underscores in numeric literals for readability. The underscores do not change the value.

python
Output
1000000
1000000
printsprints1,000,000Three values1 0 0Three printed arguments1_000_000One integer1000000One printed value
What does Python create when comma-separated values are assigned, and how is that different from writing one integer?

A Reliable Debugging Trace

Debugging a semantic error begins with the difference between what you expected and what the program actually produced. First state the expected result. Then inspect the actual output carefully. Next, trace that output back to the statement that supplied the value or values. In the comma example, the first divergence appears in the written expression 1,000,000: the commas create separate values before print() displays anything. Finally, change the statement and run the test again to verify that the output now matches the expectation.

thencompare withif differentthentest againExpected resultState what should happenActual outputRecord what happenedCompare resultsFind the first differenceTrace statementInspect the value-producingcodeCorrect expressionMatch code to intentionVerified outputRun and check again
What steps should you follow from unexpected output to a confirmed fix?

Tracing the First Divergence

A learner intends to print one million but sees 1 0 0.

State the expectation: The intended result is the single integer 1000000.

Inspect the output: The actual result is 1 0 0, which shows that three values were printed.

Inspect the statement: The commas in 1,000,000 separate 1, 0, and 0 rather than formatting one integer.

Apply the correction: Replace the comma-separated form with 1000000 or 1_000_000.

Test again: Run the corrected statement and verify that it prints 1000000.

The output matches the intention after the numeric expression is corrected.

Mistakes That Hide in Plain Sight

  • Assuming that familiar written number formatting works unchanged in Python.

    Python treats the commas as separators between values, so print() receives three arguments.

    Fix: Write print(1000000) or print(1_000_000).

  • Treating successful execution as proof that the program is correct.

    Semantic errors produce no warning even though the result is incorrect.

    Fix: Always compare the actual output with the expected output.

  • Looking only for error messages.

    Syntax errors are reported immediately, but semantic errors can run to completion.

    Fix: Inspect unexpected output and trace it back to the statement that produced it.

  • Confusing a syntax error with a semantic error.

    A missing colon violates Python's grammar, so Python refuses to run the code.

    Fix: Separate errors that prevent execution from errors that produce an incorrect result.

Practice the Trace

EASY

What will this print? Write your prediction, then explain why the output is correct or incorrect for the intended value.

Hints
  • Ask whether the commas are formatting one integer or separating values.
  • Count the values that print() receives.
  • Compare the actual output with the intended representation of one million.
python
intended asinterpreted as1000000One integer1,000,000First divergence1 0 0Three printed values
Where does the program's actual result first diverge from the expected result?

Debugging Checklist

  1. A syntax error violates Python's grammar and prevents the program from running.
  2. A semantic error runs without an error message but produces a result different from the intended result.
  3. In Python, commas separate values; they do not format the digits of one large integer.
  4. Use 1000000 or 1_000_000 for the integer one million.
  5. Debug semantic errors by stating the expectation, inspecting the actual output, tracing the first divergence to its statement, correcting the code, and testing again.

Key Takeaways

  • Syntax errors stop execution because the code does not follow Python's grammar.
  • Semantic errors are harder to notice because the code runs but produces an incorrect result.
  • The expression 1,000,000 is interpreted as the separate values 1, 0, and 0.
  • Expected-versus-actual comparison helps locate the first point where a program's behavior diverges.
  • Testing corrected code confirms whether the semantic error has been fixed.