If your code produces an error, always look at the error message that python gives you! Error messages will tell which line of code that caused the error and the type of error that occurred, usually with some helpful statement.
For example, here is some code which raises an error:
for i in range(5):
print(w)
In this error output, we see an arrow pointing to the "broken" line of code (line 2). This helps us direct our debugging efforts, because we know exactly where the problematic code is located. Sometimes, you will instead see an explicit line number printed out telling you where the error is (see examples below).
In addition, the output tells us that a NameError occurred. A NameError occurs when you attempt to use a variable that does not exist. This is exactly what python told us: "NameError: name 'w' is not defined". This error message tells us plainly that the variable w does not exist. So, we know we should modify our code so that there is a w to print, or alternatively to print an actual variable that exists.
for i in range(5):
print(i)
And now, the code works properly with no errors. There are many other types of "standard errors" like NameError (see here: https://docs.python.org/3/library/exceptions.html), but here are some ones you'll likely encounter:
Examples of these errors are shown below.
A SyntaxError occurs when you have not written a piece of code properly. These errors generally indicate that you have a typo, and they often happen when you are missing important symbols.
Python tells you exactly where the syntax problem is by explicitly stating the line in which the error occurred. Also, python actually gives a little carrot arrow to point out the exact location in the line where it found a syntax issue!
# SyntaxError example 1
# Missing a closing bracket at the end of list definition
mylist = [1,2,3,4
# SyntaxError example 2 -
# To print two values together, you need to separate them with a comma (or for strings, a plus sign)
a = "string 1"
b = "string 2"
print(a b)
# SyntaxError example 3
# missing colon when writing a for loop
fruit = ["banana", "apple", "pear", "grape"]
for f in fruit
print("This is a fruit:", f)
An IndentationError occurs when you have either inconsistent or improper indentation (i.e. the required white-space in the beginning of if statements, for loops, and functions).
# IndentationError example
if 7 > 9:
print("7 is greater than 9")
# IndentationError example. This time, indents are inconsistent across the code
for i in range(10):
print("here is the value of i:")
print(i)
A ValueError occurs when you provide a python function with an inappropriate argument.
# ValueError example
number = "mystring!"
x = int(number)
A TypeError occurs when you apply an operation to a variable of inappropriate type. They are similar to ValueErrors, although TypeErrors happen specifically when the operation being used only works on certain variable types. For instance, the len()
function is only allowed to be used on lists, dictionaries, strings, and tuples.
# TypeError example 1
a = 7
lengtha = len(a)
# TypeError example 2 - you can't subtract from strings!
a = "this is a string"
b = a - 1
A KeyError occurs when you attempt to index a key in a dictionary, but that key does not exist.
# KeyError example
d = {"seven": 7, "nine": 9, "eleven": 11}
print(d["five"])
An IndexError occurs when you attempt to index a position from a list or string, but that index isn't in the variable.
# IndexError example 1
mylist = [11, 22, 33, 44, 55, 66]
print(mylist[9])
# IndexError example 2
mystring = "This is a short string."
print(mystring[72])