Read Python Tracebacks and Find the Error
Read the error before changing code
When an exception is not handled, Python normally displays a traceback: information about where execution reached before failing. Read the final line first to find the exception type and message, then look upward for the last relevant source frame (a location in the running code). Paths, line numbers and pointer marks vary by environment.
The last line of the report identifies NameError and the undefined name quanity. The relevant source line is print(quanity). Check the spelling of the name assigned above: it is quantity. Some Python versions may also suggest a possible correction.
Now both names match and the program displays 3. Correct one cause at a time and rerun the program; a new error may become visible only after the first is fixed.
Distinguish syntax errors from execution errors
This code does not follow Python grammar and is rejected before execution. A syntax error report may point at where parsing failed rather than the exact place you need to edit. Here the correction is print("Ready"). In contrast, the earlier misspelled name is grammatically valid but fails during execution.
Find the cause of an error
Review the intentionally failing conversion int("tea") from the previous topic. Predict the exception type and identify the expression responsible before testing it. Replace the text with a valid whole number. For the next chapter you will apply arithmetic to more numeric types.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Read error reports
Check your understanding of reading tracebacks.
In an unhandled execution exception report, where should you first look for the exception type?
Which source line needs attention?
total = 5print(totl)Why does this example fail before ordinary execution?
print("Ready"What is the most useful first correction when int("tea") fails with ValueError?