Understanding errors

4.5. Understanding errors#

Sometimes the code you type into a code cell will not work. In this case, Python will not execute your code but instead print an error message. In this section, we will take a look at these error messages and learn how to understand them.

Let’s write some code that will give an error:

a = 5
printt(a)

After your code cell, you will see some colored text called a traceback. This traceback is the way in which Python tries to tell you more about the error. Let’s take a look at the traceback:

../../_images/name-error.png

Fig. 4.1 Python traceback giving more information about the error that occurred.#

The traceback contains three important details that can help you:

  1. The type of error

  2. Where the error occurred in your code

  3. An attempt to explain why the error happened

For 1 and 2, Python is rather good and will communicate clearly. For 3, sometimes you need to have some experience to understand what Python is trying to tell you.

In this specific case, the type was a NameError that occured in line 2 of our code. A NameError means that Python tried to find a function or variable that you have used, but failed to find one. If you look at the line of code, you can probably spot the problem already. At the very end of the traceback, Python tries to explain what the problem was: in this case, it is telling you that there is no function named printt.

You will also get a NameError if you try to use a variable that doesn’t exist:

print(non_existent_variable)

Another common type of error is a SyntaxError, which means you have typed something that Python does not understand:

a = a $ 5

You can also get errors if you try to use operators that do not work with the data type you have, for example, if you try to divide two strings:

"You cannot " / "divide strings"

Here, you get a TypeError - the division operator is a perfectly fine syntax, it just does not work with strings.

In Python, errors are also called exceptions, and you can find a complete list of all error (exception) types and what they mean at this link. Sometimes, you can learn more about what the error means by reading these documents, although they are perhaps a bit hard to understand for beginners.

As a last resort, you can also always try a Google search - there are lots of useful posts on Stack Exchange (where Google will also probably direct you), or you can ask ChatGPT for some help.

Exercise 4.7

Run the following three code cells and try to understand what is going wrong by reading the error message.

a = 10
b = 0
c = (a/b)
4 + nanobiology*3
d = 'nanobiology is awesome' + 2

If you are not sure what to do, what the function was called or need some help, there is a handy tool in Python: if you put a question mark before your variable or function (e.g., ?print), you get additional information. Try it out:

?print
import micropip
await micropip.install("jupyterquiz")
from jupyterquiz import display_quiz
import json

with open("questions3.json", "r") as file:
    questions=json.load(file)
    
display_quiz(questions, border_radius=0)