Make a smaller debugging example
Reduce the problem while keeping the failure reproducible.
- Record expected and actual behavior.
- Remove unrelated inputs and code until the failure remains small.
- Change one cause, rerun the same example and record the result.
If only an empty list fails, test [] separately before rewriting the whole function.
See the reasoning, step by step.
- Fictional function average([2,4]) returns 3, but average([]) raises division by zero. Record both results.
- The smallest reproducer is the empty list. Decide the intended behavior before changing code: reject it explicitly or return a documented sentinel.
- For a reject-empty contract, check that [] now raises a clear ValueError and [2,4] still returns 3. Keep both cases.
A mistake worth catching
The trap: Catching every exception and returning 0.
A better approach: Do not hide unrelated failures or invent a valid average for an empty set. Define the function contract first.
Your turn: use the idea.
Your chosen contract rejects empty input. Write two test expectations without claiming you executed them.
Try it yourself first. This is a self-review task, not automatic marking.
Compare with a model answer
average([]) should raise ValueError with a clear message; average([2,4]) should return 3. These are proposed expectations until you actually run the tests.
Check your own work
Go deeper with the right source.
Supporting references and further reading. The worked examples above are original CampusSpark teaching material.
Free language documentation; local setup and version differences matter.