Python Errors and How to Read Them
What you will learn
- Why errors are a normal part of programming
- What a syntax error, a name error and a type error look like
- How to read an error message from the bottom up
- A simple method for fixing a bug
1. Errors are normal
Every programmer, beginner or expert, sees errors every day. An error message is Python telling you what went wrong and where. Learning to read it is one of the fastest ways to improve.
The messages below come from Python 3.13. Wording differs slightly between versions, but the ideas are the same.
2. SyntaxError
A syntax error means Python could not understand how the code is written, for example a missing quote:
print("Hello)
Message:
print("Hello)
^
SyntaxError: unterminated string literal (detected at line 1)
The ^ points to where the problem starts. Here the string was never closed.
Other common syntax problems: a missing bracket, a missing colon after if (chapter 5), or the wrong indentation.
3. NameError
A name error means you used a name that Python does not know:
print(Hello)
Message:
NameError: name 'Hello' is not defined
Without quotes, Hello is read as a variable name, and no such variable exists. If you wanted text, add quotes. If you meant a variable, create it first. Typing a name with the wrong capital letter gives the same error: name and Name are different.
4. TypeError
A type error means you used a value of the wrong type for an operation:
print(5 + "5")
Message:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Python cannot add a number (int) to text (str). You must decide what you mean:
print(int("5") + 3, str(5) + "3")
Output:
8 53
int("5") turns the text into a number, so we add numerically. str(5) turns the number into text, so + joins the two pieces of text. You can check a value's type with type(...):
print(type(5), type(5.0), type("5"), type(True))
Output:
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
5. IndentationError
Python uses spaces at the start of a line to mark structure (chapter 5 uses it with if, chapter 6 with loops). A line indented when it should not be:
x = 5
print(x)
gives:
IndentationError: unexpected indent
Fix it by removing the extra space.
6. How to read a message
- Read the last line first. It names the kind of error and says what happened.
- Look at the line number and the line shown. The mistake is on that line or just before it.
- Read the pointer
^if there is one. - Change one thing and run again. Do not change many things at once.
- Search the exact message if you are stuck. Others have met it.
Worked example: fixing a bug
You write:
name = "Rina"
print("Hello, " + nane)
The last line of the error says NameError: name 'nane' is not defined. That tells you the name nane does not exist. You look at the line and see the typing slip: it should be name. You fix it:
name = "Rina"
print("Hello, " + name)
Output: Hello, Rina.
Common mistakes
- Panicking instead of reading the message.
- Reading from the top of a long error. Read the last line first.
- Changing many things at once, so you cannot tell what fixed it.
- Ignoring capital letters. Python is case sensitive.
Key takeaways
- Errors are normal, and the message tells you what went wrong and where.
- SyntaxError: the code is written wrongly. NameError: an unknown name. TypeError: the wrong type for an operation.
- Read the last line first, then the line number.
- Change one thing at a time and run again.
Practice
1. What kind of error is print("Hi" (missing closing bracket)?
Show answer
A SyntaxError, since the bracket is not closed.
2. What causes NameError: name 'total' is not defined?
Show answer
The program uses total before giving it a value, or the name is spelled differently from where it was created.
3. Fix this so it prints Total: 15: print("Total: " + 15).
Show answer
Convert the number to text: print("Total: " + str(15)), or use print("Total:", 15).
4. Which comes first when reading an error message: the top or the last line?
Show answer
Read the last line first.
5. What error does print(unknown_name) give, if unknown_name was never created?
Show answer
NameError: name 'unknown_name' is not defined.
6. What kind of error is "a" * "b" (multiplying text by text)?
Show answer
A TypeError: text can be repeated only by a whole number ("a" * 3 is fine), and Python reports can't multiply sequence by non-int of type 'str'.