Syllabus Lesson 8 of 246 · Module 1: Python for this course
Module 1: Python for this course

Reading an error message

Why it matters You will see hundreds of errors in this course. Reading them in ten seconds, instead of guessing, is a skill worth hours.

A Python error ends with a traceback. Read it from the bottom up:

  1. The last line names the kind of error and says what went wrong, for example ZeroDivisionError: division by zero.
  2. The line above it shows the exact line of your code that failed, with its line number.
  3. Lines further up show who called that function, if you need the path that led there.

See it

Traceback (most recent call last):
  File "lesson.py", line 6, in <module>
    print(average_pace([5, 10], []))
  File "lesson.py", line 2, in average_pace
    return sum(times) / len(times)
ZeroDivisionError: division by zero

Bottom line: dividing by zero. One up: line 2, len(times) was 0. So the function was handed an empty list and has no plan for it.

Traceback: the trail Python prints when it stops, newest step at the bottom.

Recap Bottom line for what, the line above for where.

Your turn

The starter code crashes. Run it, read the traceback, and fix the function so that:

  • average_pace(distances, times) returns total time divided by total distance (minutes per km).
  • If the total distance is 0, it returns None instead of crashing.

Also set error_kind to the name of the error you saw, as a string.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.