Atharva Bhagwat

Stage 2: Reading error messages

0Plan
Stage 2Core
★★

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'
  1. 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.
  2. Find your line. The arrow ----> 2 points at your code. Ignore the middle section: it is the library's own code, and the mistake is almost never there.
  3. Check what you assumed. Which column names really exist? print(df.columns) shows them, and you will see score with a small s. Names must match exactly.

The usual errors

ErrorIn plain EnglishFirst thing to try
SyntaxErrorPython could not read the line. Often a missing colon, bracket or quote.Look at the marked line and the one above it.
NameErrorYou used a name Python does not know.A typo? Or the cell that creates it has not run (kernel memory).
TypeErrorThe wrong kind of thing, such as adding text to a number.print(type(x)) for each thing involved.
ValueErrorThe right kind of thing, but a value it cannot use, such as int("abc").Print the value you passed in.
KeyErrorA column or dictionary key is not there.print(df.columns). Check spelling and capital letters.
IndexErrorYou asked for item 10 of a list with 5 items.print(len(my_list)).
FileNotFoundErrorThe path points to nothing.Check the file name and which folder you are in.
ModuleNotFoundErrorThe library is not installed here.In Colab, run %pip install name.

A routine that works

  1. Read the last line slowly.
  2. Go to the line the arrow points at. Is it what you meant to write?
  3. Check what you assumed: print(type(x)), print(df.shape), print(df.columns).
  4. Make the problem smaller: try it on two lines in a fresh cell.
  5. Suspect the kernel. Restart and run all.
  6. 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

Each pair of lines has one bug. Run the cell, read the error, fix the line, and run again. The comment says what you want.
Show a solution (try it first)

The output should look like this:

expected output
p.Leu25Pro
3.49
81.66666666666667
one solution
names = ["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

Run the cell below. It fails on purpose. In a text cell, write down: (1) the type of error, (2) the line it points to, and (3) how you would fix it.
Show a solution (try it first)
one solution
# fix: the key is spelled score, with a small s

Check your understanding

First star

Pick the best answer for each.

Which line of an error message tells you what went wrong?
KeyError: 'Score' after df["Score"]. The file's column is called score. What is the fix?
You uploaded toy.csv in Colab, and pd.read_csv("data/raw/toy.csv") gives FileNotFoundError. Most likely cause?
total = 0, then for s in ["1.5", "2.5"]: total = total + s gives a TypeError. What is s?

Stuck? Get a nudge

Question 2: compare the name in the error with the real column name, letter by letter.

Special thanks to Nilay!