Learning guides →

· Albion Gateway College

Python module practice: read a traceback before changing code

Practise exception diagnosis with a small mean function, distinguish empty inputs from type errors, and prepare a reproducible debugging question.

Read the input as well as the exception

Keep the final exception line, locate the relevant frame in your own code and record the input. The Python tutorial explains that the exception message identifies its type while the traceback gives context.

For a university tutorial, supply the Python version, smallest reproducing input, expected output and actual output. A copyable example is easier to diagnose than an isolated screenshot.

Worked exercise: one function, two failures

Consider a function whose body is return sum(values) / len(values). For [3, 5], calculate 4.0 yourself. For [], the denominator is zero and the exception is ZeroDivisionError: the contract for an empty input needs attention.

For [3, "5"], summing attempts incompatible addition and raises TypeError before division. This is a different failure and requires a decision about accepted input types.

Choose a contract before a repair

If the function accepts only non-empty numeric lists, it can explicitly reject an empty input with an informative ValueError. If data arrives as text, decide at the input boundary how conversion failures should be reported.

Catching every exception and returning zero hides the distinction between valid zero and failure. Replacing missing data with zero also changes the meaning of the calculation.

Check both successful and failing cases

Create a table for [3, 5], [-2, 2], [] and [3, "5"]. The first two results should be 4.0 and 0.0. Define the expected rejection or conversion policy for the other two before running them.

A useful tutorial outcome is being able to explain both your diagnosis and your tests. Students must independently produce assessed code under their module rules.

References

Your next step

For a module-support enquiry, describe the module, learning goal and what you have tried. Appropriate teaching availability requires confirmation; assessed work remains your own.

Continue reading

College online consultation

Welcome

Hello and welcome! Are you a student, an admissions partner, a job seeker, or applying to become a tutor? Please tell us what you would like help with.

[email protected]