Python Programming · Unit 6 of 8
Unit 6: Errors, debugging and testing
Reading tracebacks from the bottom up, common exception types, try and except, raising your own errors, a step-by-step debugging method, and testing with assert and unittest.
Opens the first lesson of this unit in Python Programming. Every lesson is open from the start.
Lesson 1: Reading tracebacks
Goal: Read a traceback from the bottom up, find the line that failed and name common exception types.
When Python hits a problem it cannot handle while running, it raises an exception and, if nothing catches it, stops and prints a traceback: a report of what went wrong and where. Beginners often panic at the wall of text; professionals read it in seconds, because it always has the same shape.
Traceback (most recent call last):
File "main.py", line 5, in <module>
print(last_item(basket))
~~~~~~~~~^^^^^^^^
File "main.py", line 2, in last_item
return items[len(items)]
~~~~~^^^^^^^^^^^^
IndexError: list index out of range
Read it from the bottom. The last line names the exception type and a message: IndexError: list index out of range. Above it are the frames, the chain of calls that led there, oldest first and most recent last ("most recent call last"). Each frame shows the file, the line number, the function (<module> means the top level of the file), the line of code itself, and often ~ and ^ marks under the exact part that failed. The last frame is where the error was raised; the frames above it tell you how the program got there.
Frames from libraries, such as Python's own json or csv modules, can appear below your code. The bug is usually in your code, in the last frame that belongs to your files: you probably passed the library something it could not handle. And sometimes the line that failed is not the line that is wrong: if a variable is unexpectedly None on line 9, the real mistake may be on line 4, where it was set.
| Exception | Usually means | Typical example |
|---|---|---|
SyntaxError | the code breaks Python's grammar; nothing ran | missing colon, bracket or quote |
IndentationError | the indentation does not make sense | a block that is not indented |
NameError | a name is not defined (often a typo) | prnt("hi") |
TypeError | an operation got the wrong type | "Age: " + 30 |
ValueError | right type, unusable value | int("4.5") |
IndexError / KeyError | a position or key that does not exist | items[10], d["missing"] |
AttributeError | no such method or attribute | None.upper(), nums.push(1) |
ZeroDivisionError | division by zero | averaging an empty list |
Syntax errors are different: they are found while Python reads the file, before anything runs, so no frames from running code appear. Python points at where it got confused, which is often just after the real mistake, such as a missing bracket or colon on the line above.
Worked example: reading the traceback above in four questions
- What kind of error? The last line says
IndexError: list index out of range: a position that does not exist. - Where was it raised? The last frame: line 2, inside
last_item, atitems[len(items)]; the^marks point at the index. - How did we get there? The frame above: line 5 called
last_item(basket)from the top level of the file. - Why?
baskethas 2 items, solen(items)is 2, but the valid positions are 0 and 1. The fix isitems[-1].
Type, place, route, reason: four questions that work on any traceback, however long. The lab below asks the same questions about nine real crashes.
Hands-on lab in the room: Traceback reader. Run the program, read the raw traceback, and answer: which line, what kind of error, which fix? The full traceback reader has nine of these.
- Exception
- An error raised while a program runs; it stops the program unless it is caught.
- Traceback
- Python's report of an uncaught exception: the chain of calls, then the error type and message.
- Frame
- One entry in a traceback: a file, line and function in the chain of calls.
- SyntaxError
- An error found while reading the code, before it runs, because it breaks Python's grammar.
- Runtime error
- An exception raised while the program is running, such as TypeError or KeyError.
Remember: Read tracebacks from the bottom: the last line is the error, the last frame is where it happened. Check the line above it too.
Lesson 2: Handling and raising exceptions
Goal: Catch specific exceptions with try and except, use else and finally, and raise clear errors of your own.
Some errors are not bugs but facts of life: users type "ten" where you asked for a number, files contain broken rows, a network call fails. Instead of crashing, you can catch the exception and decide what to do. Put the risky code in a try block and the recovery in an except block that names the exception you expect.
def parse_qty(text):
try:
qty = int(text) # only the line that can fail
except ValueError:
return None # recovery: signal "not a number"
return qty
for raw in ["12", "ten", " 3 "]:
print(repr(raw), "->", parse_qty(raw))
Catch specific exceptions, and keep the try block small. A bare except: (or except Exception: around a big block) also catches your own typos and bugs, which then fail silently and are much harder to find. You can catch several types, except (ValueError, TypeError):, use several except blocks, and get the exception object with as: except ValueError as e: print("Bad row:", e).
| Block | Runs when |
|---|---|
try: | always: the code that might fail |
except SomeError: | only if that exception was raised in the try block |
else: | only if no exception was raised (keeps the try block small) |
finally: | always, error or not: for clean-up such as closing a file |
You can also raise exceptions yourself with raise ValueError("amount must be positive"). That is how a function says "I cannot do what you asked", and it is far better than returning a misleading value such as -1 or printing a message nobody sees. Choose the exception that fits: ValueError for a bad value, TypeError for a wrong type, KeyError for a missing key. Inside an except block, a bare raise re-raises the same exception after you have logged it.
rows = ["pens,3", "ink,two", "pads,5", "tape"]
total = 0
problems = []
for n, row in enumerate(rows, start=1):
try:
name, qty = row.split(",")
qty = int(qty)
except ValueError as e:
problems.append(f"row {n}: {e}")
else:
total += qty
print("Total:", total)
for p in problems:
print(p)
Worked example: loading rows, keeping the good and reporting the bad
- Row 1 splits and converts fine, so the else block adds 3.
- Row 2:
int("two")raises ValueError, which is caught and recorded with its message. - Row 4 has no comma, so unpacking one piece into two names raises ValueError ("not enough values to unpack"), also recorded.
- The loop carries on after each problem, and the good rows still count.
Output: Total: 8, then two problem lines naming the row and the reason. This is the pattern behind every data import at work.
Hands-on lab in the room: Python playground.
Hands-on lab in the room: Unit 6 coding exercises. Graded exercise: raise clear errors with the right exception type.
- try / except
- A statement that runs code and catches a named exception if one is raised.
- raise
- A statement that raises an exception, such as raise ValueError("bad amount").
- else (in try)
- A block that runs only if the try block raised no exception.
- finally
- A block that always runs after try, whether or not an exception happened.
- Bare except
- An except with no exception named; it hides real bugs and should be avoided.
Remember: Catch specific exceptions around the smallest risky code. Raise clear exceptions instead of returning odd values.
Lesson 3: A debugging method
Goal: Debug systematically: reproduce, read, form a hypothesis, inspect, shrink the problem, fix and test.
Debugging is a skill with a method, not a talent. Developers who seem fast at it are not guessing better; they are following steps. When something is wrong, resist the urge to change code at random. Instead:
- Reproduce it. Find the exact input that causes the problem, so you can see it happen every time.
- Read the evidence. The traceback, the wrong output, the expected output. What exactly differs?
- Form a hypothesis. "I think
totalis a string, not a number, by line 6." A good hypothesis can be checked. - Inspect. Print the values that matter, with types:
print(f"{total=} {type(total)=}"). Or step through in a debugger, or in this course's tracer. - Shrink the problem. Cut the input or the code in half until the smallest version that still fails is obvious.
- Fix the cause, not the symptom, then rerun the original failing case and a few normal cases to make sure nothing else broke.
Explaining the code out loud, line by line, to a colleague or even a rubber duck on your desk, finds a surprising number of bugs: saying what each line is supposed to do makes you notice when it does not. Professional tools add breakpoints (pause the program at a line and look at every variable) and logging (permanent, switchable messages instead of throwaway prints), but the method stays the same.
| Symptom | Likely cause |
|---|---|
| Off by one item, or the first/last item missing | range(1, n) or range(n - 1) instead of range(n); < versus <= |
| A condition is always true | x == "a" or "b"; a non-empty string is truthy |
| A list changes "by itself" | aliasing (b = a) or a mutable default argument |
| A function gives None | a missing return, or return indented inside a loop |
| Numbers join like text ("12" + "3" = "123") | values from input, files or split() never converted |
| A loop stops after one round | return or break in the wrong place, often in an else branch |
def average(nums):
total = 0
for i in range(1, len(nums)):
total += nums[i]
return total / len(nums)
print(average([2, 4, 6])) # prints 3.333..., expected 4
Worked example: debugging a wrong average
- Reproduce:
average([2, 4, 6])gives 3.33 instead of 4. A smaller case,average([5]), gives 0.0: even more obviously wrong. - Hypothesis: the total is too small, so some number is not being added.
- Inspect:
print(i, nums[i])inside the loop shows only 1 4 and 2 6. Index 0 is never visited. - Fix the cause: loop over the items themselves,
for n in nums: total += n, then rerun [2, 4, 6], [5] and [1, 2].
Shrinking to [5] made the bug obvious. Small, strange inputs are a debugger's best friend.
Hands-on lab in the room: Debugging lab with planted bugs. Try the method on a planted bug. The full debugging lab has eight.
- Bug
- A mistake in code that makes it behave differently from what was intended.
- Reproduce
- Find inputs and steps that make a bug happen reliably.
- Hypothesis
- A specific, checkable guess about the cause of a bug.
- Breakpoint
- A marker that pauses a program at a line in a debugger so you can inspect its variables.
- Rubber duck debugging
- Explaining code line by line, out loud, to find where it stops doing what you think.
Remember: Reproduce, read, hypothesise, inspect, shrink, fix the cause, retest. Never change code at random.
Lesson 4: Testing with assert and unittest
Goal: Write tests with assert and unittest that cover normal cases and edge cases, and explain why tests matter at work.
A test is code that checks other code. Instead of running your program and eyeballing the output each time you change something, you write down what the right answers are, once, and let the computer check them in a second. At work, code without tests is hard to change safely, so teams expect tests with every change, and an automated system usually runs them all before new code is accepted.
The simplest test is an assert statement: assert condition, "message". If the condition is true, nothing happens; if it is false, Python raises AssertionError with your message. Group asserts for one function in a test function whose name starts with test_, a convention testing tools rely on.
def slugify(title):
"""'Hello World!' -> 'hello-world'"""
kept = "".join(ch for ch in title.lower() if ch.isalnum() or ch == " ")
return "-".join(kept.split())
def test_slugify():
assert slugify("Hello World") == "hello-world" # normal case
assert slugify(" Many spaces ") == "many-spaces" # extra spaces
assert slugify("Python 3 Tips!") == "python-3-tips" # digits and punctuation
assert slugify("") == "" # edge case: empty
test_slugify()
print("all tests passed")
What to test. One or two normal cases, then the edge cases where bugs hide: empty input, zero, one item, negative numbers, the exact boundary values (< versus <=), duplicates and ties, unusual text (spaces, capitals, punctuation). And test that errors are raised when they should be. A good test is one that would fail if the code were wrong: a test that can never fail proves nothing.
Python's standard library includes unittest, a fuller testing framework. You group tests as methods of a class that inherits from unittest.TestCase, and use methods such as assertEqual(a, b) (which shows both values when they differ), assertTrue(x) and assertRaises(ValueError). Many teams use the popular third-party tool pytest, which runs plain test_ functions with bare asserts; you will install it with pip (Unit 8).
import unittest
def withdraw(balance, amount):
if amount <= 0:
raise ValueError("amount must be positive")
if amount > balance:
raise ValueError("insufficient funds")
return balance - amount
class TestWithdraw(unittest.TestCase):
def test_normal(self):
self.assertEqual(withdraw(100, 30), 70)
def test_everything(self):
self.assertEqual(withdraw(50, 50), 0)
def test_refuses_too_much(self):
with self.assertRaises(ValueError):
withdraw(20, 30)
unittest.main(argv=["tests"], exit=False)
Worked example: the same idea with unittest
- Each method starting with
testis one test, run separately. assertEqualreports both values if they differ, which makes failures quick to understand.with self.assertRaises(ValueError):passes only if the indented code raises ValueError.unittest.main(argv=["tests"], exit=False)runs the tests inside this page; in a real project you would runpython -m unittestfrom a terminal.
The output ends with Ran 3 tests and OK. Change withdraw so it stops checking the balance and run it again to watch a test fail.
Hands-on lab in the room: Python playground. unittest writes its report to stderr, so it appears in the error colour; that is normal.
Hands-on lab in the room: Unit 6 coding exercises. Graded exercise: write a test good enough to catch five broken versions of a function.
- Test
- Code that checks that other code gives the expected results.
- assert
- A statement that raises AssertionError if its condition is false.
- Edge case
- An unusual input at the limits, such as empty, zero, one item or a boundary value.
- unittest
- Python's built-in testing framework, with TestCase classes and methods such as assertEqual.
- pytest
- A popular third-party testing tool that runs plain test_ functions with bare asserts.
Remember: Write tests that would fail if the code were wrong: normal cases, edge cases and expected errors. Run them after every change.
Practise it in Python Programming
Each lesson ends with three questions that check you can use the idea, not just recognise it. Wrong answers explain why and come back in Review. The unit ends with a 7-question quiz; pass it with 80% or more to master the unit. Hands-on lab: Traceback reader, Debugging lab with planted bugs and Unit 6 coding exercises.
Every lesson is free: no ads, no lives and no paywall.