Stage 2: Reading error messages
Everyone sees red error text all day. The skill is reading it quickly, and it is easier than it looks.
An error message is the computer telling you, as clearly as it can, where it got stuck and why. Here is a real one:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[4], line 2
1 df = pd.read_csv("toy.csv")
----> 2 print(df["Score"].mean())
... (many lines from inside pandas) ...
KeyError: 'Score'- Start at the bottom.
KeyError: 'Score'says you asked for something by name and it is not there. The first word is the kind of error; the rest is the detail. - Find your line. The arrow
----> 2points at your code. Ignore the middle section: it is the library's own code, and the mistake is almost never there. - Check what you assumed. Which column names really exist?
print(df.columns)shows them, and you will seescorewith a small s. Names must match exactly.
The usual errors
| Error | In plain English | First thing to try |
|---|---|---|
SyntaxError | Python could not read the line. Often a missing colon, bracket or quote. | Look at the marked line and the one above it. |
NameError | You used a name Python does not know. | A typo? Or the cell that creates it has not run (kernel memory). |
TypeError | The wrong kind of thing, such as adding text to a number. | print(type(x)) for each thing involved. |
ValueError | The right kind of thing, but a value it cannot use, such as int("abc"). | Print the value you passed in. |
KeyError | A column or dictionary key is not there. | print(df.columns). Check spelling and capital letters. |
IndexError | You asked for item 10 of a list with 5 items. | print(len(my_list)). |
FileNotFoundError | The path points to nothing. | Check the file name and which folder you are in. |
ModuleNotFoundError | The library is not installed here. | In Colab, run %pip install name. |
A routine that works
- Read the last line slowly.
- Go to the line the arrow points at. Is it what you meant to write?
- Check what you assumed:
print(type(x)),print(df.shape),print(df.columns). - Make the problem smaller: try it on two lines in a fresh cell.
- Suspect the kernel. Restart and run all.
- Search the web with the error type and the library, for example pandas KeyError column, and read why it happens before copying a fix.
Now practise in Colab
You have read the ideas. Next, try them. The notebook repeats the code above with a few blanks for you to fill in, then ends with small exercises. It is all about this stage. Run the cells in order.
Open the practice notebookYou need a free Google account. When it opens, choose File, then Save a copy in Drive, so your work is yours.
Exercises
These are also in the notebook. Try each one yourself before you open the solution.
Fix three small bugs
Show a solution (try it first)
The output should look like this:
p.Leu25Pro
3.49
81.66666666666667names = ["WT", "p.Trp18Ter", "p.Leu25Pro"]
last_name = names[2]
print(last_name)
scores = {"WT": 3.49, "p.Trp18Ter": 1.5}
print(scores["WT"])
reads = [120, 80, 45]
print(sum(reads) / len(reads))Read an error message
Show a solution (try it first)
# fix: the key is spelled score, with a small sCheck your understanding
First star
Pick the best answer for each.
Stuck? Get a nudge
Question 2: compare the name in the error with the real column name, letter by letter.
Second star
This code has three bugs. Paste it into a cell, run it, and fix what each error tells you, one at a time. When it works it prints an average. What is that average, to 3 decimal places?
scores = [3.45, 1.55, 2.18, 2.59, 3.82]
total = 0
for s in scores
total = total + s
mean = total / len(score)
print("average: " + mean)Stuck? Nudge and solution
Expect three different errors, in this order: SyntaxError (something missing at the end of a line), NameError (one name is misspelled), then TypeError (you cannot join text and a number with +). For the last one, print the number on its own: print("average:", mean).
scores = [3.45, 1.55, 2.18, 2.59, 3.82]
total = 0
for s in scores:
total = total + s
mean = total / len(scores)
print("average:", mean)Special thanks to Nilay!