When an exception is not handled, Python usually displays a traceback: a record of active calls leading to the error. Consider a file named trip.py:
def per_person(total, people):
return total / people
def report():
return per_person(12, 0)
print(report())
One actual run produced the following traceback (the directory in this example is specific to that run):
Traceback (most recent call last):
File "/home/qv/.hermes/cache/scratch/tmp0fpk0l3t/trip.py", line 7, in <module>
print(report())
~~~~~~^^
File "/home/qv/.hermes/cache/scratch/tmp0fpk0l3t/trip.py", line 5, in report
return per_person(12, 0)
File "/home/qv/.hermes/cache/scratch/tmp0fpk0l3t/trip.py", line 2, in per_person
return total / people
~~~~~~^~~~~~~~
ZeroDivisionError: division by zero
Running python trip.py raises ZeroDivisionError. The traceback lists the file and line at the top-level call, then the call inside report, then the division inside per_person. The final line identifies the exception and message: ZeroDivisionError: division by zero. Formatting, paths, and line indicators vary with Python version and environment.
Read from the final exception upward to find the operation and the calls that supplied its inputs. Here the immediate problem is a zero divisor; whether to reject zero, use a different result, or avoid the call depends on the intended behavior.
A SyntaxError is different: Python cannot parse code as written, for example when a closing quote is missing. An IndentationError is a kind of syntax error involving indentation. Python points near where it detected the problem, which can be after the actual mistake. Use consistent spaces; four per level is standard. Fixing one syntax error can reveal another on the next run.
A small debugging progression shows why another run matters. Suppose we intend to divide 12 items among 3 people:
total = 12
people = 0
print(totals / people)
The first run raises NameError: name 'totals' is not defined: the assignment uses total, not totals. Correcting that name exposes a second issue because the divisor was also accidentally entered as zero:
total = 12
people = 0
print(total / people)
This run raises ZeroDivisionError: division by zero. The name fix did not cause the zero; the earlier exception prevented execution from reaching division. Checking the intended input and changing people to 3 gives:
total = 12
people = 3
print(total / people)
4.0
A NameError means a name could not be found; TypeError can mean an operation received an incompatible type. An exception identifies a symptom, not necessarily the underlying design problem. Preserve a small reproducible example and the full traceback when seeking help, after removing sensitive information.