Most common Python errors that beginners meet fall into a handful of types, and the last line of each traceback names the type and gives a short explanation that usually points straight at the fix. The examples below were run in Python 3.13, so the wording on an older version may differ slightly, and the descriptions of each exception come from the official Python documentation (Python Software Foundation, n.d.-a).

Reading the last line first

A traceback lists the file and line number where Python stopped, often with carets under the part of the line that caused the problem, and finishes with the exception name and message. The Python tutorial separates syntax errors, which stop a program before it runs, from exceptions, which are raised while it is running (Python Software Foundation, n.d.-b), and that split is a useful first question to ask whenever an error appears.

SyntaxError

Python raises a SyntaxError when the parser cannot make sense of a line, and recent versions often say exactly what is missing.

print("hi"
SyntaxError: '(' was never closed

Unclosed brackets and quotes, a missing colon after if or def, and = written where == was meant are frequent causes. The line Python reports is sometimes the one after the real mistake, so check the line above as well when the reported one looks correct.

IndentationError

The documentation describes IndentationError as the base class for syntax errors related to incorrect indentation, which makes it a subclass of SyntaxError (Python Software Foundation, n.d.-a).

if True:
print(1)
IndentationError: expected an indented block after 'if' statement on line 1

Any block that follows a colon needs indenting, and PEP 8 recommends four spaces per level (van Rossum et al., 2001). A related TabError appears when tabs and spaces are mixed inconsistently, which is why most editors can be set to insert spaces when you press Tab.

NameError

A NameError is raised when a local or global name is not found (Python Software Foundation, n.d.-a), which in practice means a typo, a variable used before it was assigned, or a missing import.

prnt("hi")
NameError: name 'prnt' is not defined. Did you mean: 'print'?

The "Did you mean" suggestion that recent versions add is often right, and when the name is spelled correctly, check whether it was defined inside a function you are calling it from outside of, or in a part of the script that has not yet run.

TypeError

Python raises a TypeError when an operation or function is applied to an object of inappropriate type (Python Software Foundation, n.d.-a), and joining text to a number is the classic beginner case.

x = "Age: " + 21
TypeError: can only concatenate str (not "int") to str

Converting the number with str(21) or using an f-string such as f"Age: {age}" fixes this one. Values read with input() arrive as strings, so arithmetic on them raises a similar TypeError until you convert them with int() or float().

IndexError and KeyError

An IndexError is raised when a sequence subscript is out of range, and a KeyError when a dictionary key is not found in the set of existing keys (Python Software Foundation, n.d.-a).

l = [1, 2, 3]
l[3]
IndexError: list index out of range

Lists start counting at zero, so a three-item list has indexes 0, 1 and 2, and loops written with range(len(l) + 1) run one step too far. For dictionaries, d.get("b") returns None instead of raising a KeyError when the key might be missing.

ValueError

A ValueError means a function received an argument of the right type but an inappropriate value (Python Software Foundation, n.d.-a), such as text that cannot be read as a number.

int("abc")
ValueError: invalid literal for int() with base 10: 'abc'

This often comes from user input, and the tutorial shows how a try and except ValueError block can ask again instead of letting the program stop (Python Software Foundation, n.d.-b).

When the message is not enough

Printing the values just before the failing line, or stepping through the code, usually shows why a variable holds something unexpected, and the guide on how to debug Python code walks through both approaches. The guide on how to write a Python script for a class covers structuring a script so errors are easier to trace. If you are stuck on a stubborn traceback in coursework, coding support and assignment support can explain what is going wrong so you can fix it yourself.

Frequently asked questions

Why does Python report the wrong line for a SyntaxError?

The parser reports where it realised something was wrong, which can be after an unclosed bracket or quote that started earlier, so the real mistake is often on the line above.

What is the difference between an error and an exception in Python?

The tutorial uses syntax errors for problems found before the code runs and exceptions for errors detected during execution, many of which a program can handle with try and except (Python Software Foundation, n.d.-b).

Should I wrap everything in try and except to stop errors?

Catching every exception hides the cause of bugs, so it is better to catch specific exceptions such as ValueError where you expect them and fix the others.

References

Python Software Foundation. (n.d.-a). Built-in exceptions. Python 3 documentation. https://docs.python.org/3/library/exceptions.html

Python Software Foundation. (n.d.-b). Errors and exceptions. The Python tutorial. https://docs.python.org/3/tutorial/errors.html

van Rossum, G., Warsaw, B., & Coghlan, A. (2001). PEP 8: Style guide for Python code. Python Enhancement Proposals. https://peps.python.org/pep-0008/