
When something goes wrong with your program, Python will give you an error message. These messages are meant to help you find where your error is and what kind of error it is.
NameErrorImportErrorValueErrorTypeErrorUnboundLocalErrorPython has many, many error messages. It can take a long time to learn them all. When you encounter an error message you are unfamiliar with, you should first read and think about the message. If you are not sure what it means, you should look up the error’s meaning in the documentation. Then, you can debug your program. The list shown here is just a small subset of all the different kinds of errors.
Just a minor point of terminology: errors are sometimes referred to as exceptions. They’re not quite identical, but for our purposes they might as well be.
Let’s imagine that we ran some code, and Python reported an error. For example, running the expression 1 + "" produces this traceback, so called because it “traces back” through the code to show you where the error occurred:
Traceback (most recent call last):
File "answer.py", line 1, in <module>
1 + ""
TypeError: unsupported operand type(s) for +: 'int' and 'str'
On the last line, you can see the type of error. In this example, we have a TypeError. After the error type, there is usually some further description of the error — here, that adding (+) an int and a str is not supported.
It can be tricky to read, but the traceback should also tell you where the error occurred. Looking at the same traceback again:
Traceback (most recent call last):
File "answer.py", line 1, in <module>
1 + ""
TypeError: unsupported operand type(s) for +: 'int' and 'str'
The File "answer.py", line 1 part identifies the file and line number where the error happened, and the line below it shows the actual code that caused it. Of course, sometimes the error is actually caused by code on a previous line instead of the line shown. You will have to think critically about whether the reported line is really where the mistake is.
student_grade = 10 # Note the typo below! print(studten_grade)
Let’s look at a few common errors. First, the code shown here causes a NameError. A NameError comes up when you try to reference a variable that does not exist yet. Sometimes, you misspelled the name of the variable. Sometimes, you have not initialized the variable. Sometimes, you initialized the variable, but only after its first usage. In this case, there is a typo in the name student_grade. Try running the code to see the error.
print("7"+3)
Another common error is the TypeError, like in the code shown here. A TypeError occurs when you use an operator incorrectly. For example, adding together a string and a number is not allowed. When you read the error message that is produced, it will describe what went wrong. Here, it says that we attempted to concatenate (which means combine) a string and an integer.
Remember that there are five basic types we have learned so far (integer, float, string, Boolean, and the special None type). The various operators have their own special rules for whether an operation is valid on specific types. Sometimes, these rules can be very surprising, like the following examples.
You can add integers to integers and strings to strings, but you cannot add integers to strings and vice versa.
You can multiply integers and floats, and you can also multiply an integer by a string (it will repeat the string that many times). However, you cannot multiply two strings.
You can always use == and != to test the equality/inequality of any two values, even if they are different types (although strings and integers will never be equal).
You can only use the order comparison operators (<, >, <=, >=) to check the order of numerics (integers and floats) with other numerics, or strings with other strings. You cannot mix strings and integers with the order operators.
You can test if one string is in another string, but you cannot use the in operator with integers.
name = "Klaus von Wagglyschwantz"
age = 17
is_good_dog = True
print("Klaus is a good dog?)
print(is_good_dog)
A SyntaxError means that you broke Python’s rules of spelling, grammar, and punctuation. There are many ways to break a program’s syntax. Usually, Python is very good at suggesting where the error is. Still, sometimes it can be very tricky to track down a SyntaxError. You usually want to look at the line where the error occurs and also the previous line of actual code.
Can you spot the SyntaxError in the code shown? The second string literal ("Klaus is a good dog?") is missing its ending double quote! Often, a SyntaxError occurs because you drop a single character, forget to close a parentheses or string literal, or some other silly mistake.

We’ll learn some more techniques to help you fix your broken programs later. For now, think critically about the errors you receive. When you see one you don’t understand, ask “What does this error message mean?” instead of just “What is wrong with my program?” Treat each error message as a mystery and yourself as a detective.
TypeError, NameError, and ValueError.