Understanding errors

5.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. 5.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.

AI tip

When you encounter a confusing error message, try copying it along with your code into an LLM and ask for a plain English explanation. AI can often clarify what the error means and suggest how to fix it.

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 5.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

AI tip

ChatGPT and other chatbots can be very helpful when resolving errors. They can, for example, explain the error when it is written in a complicated way, and even provide an explanation on how to solve it. Be aware that the solution provided by ChatGPT might be incorrect or incomplete. In cases where it is correct, it does not have to be the only possible solution. That being said, using ChatGPT can save a lot of frustration to understand what errors you are working with.

Exercise 5.8

Run the following code cells and try to understand what is going wrong by reading the error message. If you don’t understand the error message or do not see how to solve it, you can try using ChatGPT to explain it to you!

x, y = [1]
x = 10
x.append(2)
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)