Fix the bug: read the traceback
So far every lesson asked you to write code from scratch. Real work is more
often the opposite: someone else's code is already there, and it's broken.
The skill that matters is reading the traceback Python hands you - it names
the exact line and the exact exception, if you read it instead of guessing.
The code below throws as soon as you press Run - it isn't waiting for a test
to catch it, the bug crashes it immediately. IndexError: list index out of range means the loop asked for an index that doesn't exist in the list - and
the line number in the traceback points straight at the loop that asked for
it.
Your task: run the code, read the traceback, and fix the bug in
average so it returns the mean of the numbers in nums.
You'll practice:
Reading a traceback instead of guessing where the bug is
Tracing an IndexError back to an off-by-one loop bound
Show a hint
Show solution
Previous Next