Concepts / Understanding Error Messages and Debugging

Understanding Error Messages and Debugging

Python is not intelligent and cannot guess your intent. It is strict about syntax and will reject any code that violates its rules, even by a single character.

  • Programming

When Python Refuses to Guess

A person can often understand a sentence that contains a typo or a missing word. Python does not work that way. Python is not intelligent in the sense of guessing what you meant. It understands Python syntax, and it rejects code that violates the rules, even when the violation is only a single character.

submitvalid syntaxinvalid syntaxinspectlocate causetry againWrite codePython checks syntaxExecute codeSyntaxErrorRead messageFix mistake
What happens next as you write code, encounter an error, read the message, and fix the mistake?

Valid Syntax as a Conversation

Python acts as an intermediary between programmers and users. A programmer writes instructions in Python syntax. Python executes those instructions and produces output for the user. If the instructions do not follow Python's syntax rules, Python stops and reports an error instead of running the program.

python
Output
Hello world!

This instruction uses the expected pattern for displaying text: the word print, parentheses, and text inside matching quotation marks. Because the syntax is valid, Python recognizes the instruction and displays the text.

Reading a SyntaxError

A SyntaxError means that code has violated Python's syntax rules. The message gives information about the file and line where the error occurred, shows the relevant line of code, and uses a pointer marked with a caret to show approximately where the problem was detected. It also names the error and describes what went wrong.

showspoints intodescribesexplainsLine 1reported locationprint('say)code Python examined^approximate problempositionSyntaxErrorunterminated string literal
How do the line number, code line, and caret point you toward the likely problem?

Treat the message as a set of clues rather than as an instruction to guess. First find the reported line. Then inspect the code shown on that line. Next look at the caret and read the description of the error. Together, these details identify where Python detected a problem and what rule the code failed to follow.

One Character Can Stop Execution

Python rejectsPython executesprint('say)print('say')SyntaxErrorDisplayed text
What changes when one required quote is added to a string?

The difference between the two lines is one closing quote. That single character changes whether Python can recognize the string. With no closing quote, Python rejects the entire line. After the quote is added, the string has the matching punctuation Python expects.

python
Output
say
  • Forgetting parentheses after print violates the syntax expected for that command.
  • Leaving a string without matching quotation marks creates an unterminated string literal.
  • Writing natural-language instructions instead of Python commands does not give Python valid syntax.

Mistakes Beginners Make

  • Expecting Python to understand almost-correct English.

    Python does not understand English sentences as Python instructions. It understands Python syntax.

    Fix: Express the instruction using the appropriate Python syntax, such as print followed by parentheses and quoted text.

  • Ignoring the line number in the error message.

    The line number tells you where Python detected the problem and narrows the code you need to inspect.

    Fix: Go to the reported line, read the code shown there, and compare it with the error description.

  • Treating the caret as an exact explanation of intent.

    The caret points approximately to where the problem was detected. The error name and description also matter.

    Fix: Use the line, the displayed code, the caret, and the description together.

  • Stopping after seeing an error.

    Errors are part of the syntax-learning cycle. The message provides information that can help you correct the code.

    Fix: Read the message, identify the mistake, fix it, and try the code again.

When debugging a SyntaxError, do not begin by changing several unrelated parts of the program. Begin with the information Python supplied: the error name, the reported line, the displayed code, the caret, and the description. Then make the correction suggested by those clues and try again.

Practice the Correction Cycle

EASY

Imagine that Python reports an unterminated string literal for a line that begins with print and contains an opening single quote. Describe the sequence you would follow to find and correct the mistake.

Hints
  • Start with the line number in the message.
  • Inspect the quotation marks on that line.
  • Check whether the string has a matching closing quote.
  • Try the corrected line again.
MEDIUM

A learner says, Python knows I wanted to display a greeting, so it should be able to run my sentence. Explain why this expectation is incorrect and identify the kind of syntax the learner should use instead.

Hints
  • Separate the programmer's intention from the exact instruction Python can interpret.
  • Recall the syntax used to display text.
  • Mention the word print, parentheses, and quoted text.
  1. Debugging syntax is a repeatable process, not a sign that Python has misunderstood a reasonable request. Write the code, let Python accept or reject it, read the message when it rejects the code, locate the reported line and approximate position, correct the syntax, and try again.

Key Takeaways

  • Python follows strict syntax rules and does not guess a programmer's intention.
  • A SyntaxError means that the code violates Python's syntax rules.
  • The error message provides an error name, a reported line, the relevant code, a caret, and a description of the problem.
  • A missing character, such as a closing quote, can cause Python to reject an entire line.
  • Debugging follows a cycle: write code, read the error, locate the mistake, fix it, and try again.