Level 2 / Project 04 - Error Safe Divider¶
Home: README
Try in Browser: Run this exercise online — no installation needed!
Before You Start¶
Recall these prerequisites before diving in:
- Can you write a try/except block that catches a specific exception type?
- Can you explain the difference between ValueError and TypeError?
Estimated time: 35 minutes
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | Diagram | Browser |
Focus¶
- exception handling and graceful failure
Why this project exists¶
This project gives you level-appropriate practice in a realistic operations context. Goal: run the baseline, alter behavior, break one assumption, recover safely, and explain the fix.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-2/04-error-safe-divider
python project.py --input data/sample_input.txt
python project.py --interactive
pytest -q
Expected terminal output¶
Expected artifacts¶
- Division results and summary on stdout
- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add support for integer division (
//) with a--integerflag. - Add a
--precisionargument to control decimal places in results. - Return results sorted by value (largest first) when
--sortedis passed.
Break it (required) — Core¶
- Pass
float('inf')as a numerator — what result do you get? - Pass an extremely large number — does Python overflow?
- Use a file with no valid operations — does the summary crash?
Fix it (required) — Core¶
- Add a check for
float('inf')andfloat('nan')results. - Handle the empty-results case in
summarise_results. - Add tests for infinity and NaN edge cases.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- What is the difference between
except ValueErrorand a bareexcept? - Why is catching specific exceptions better than catching
Exception? - How does
try/except/else/finallywork — what runs when? - When would error-safe patterns like this be critical in production?
Mastery check¶
You can move on when you can:
- list 5 built-in exception types and when each occurs,
- explain exception hierarchy (BaseException vs Exception),
- write a try/except that catches multiple specific types,
- describe why bare except: is dangerous.
Related Concepts¶
- Collections Explained
- Errors and Debugging
- Functions Explained
- How Loops Work
- Quiz: Collections Explained
Stuck? Ask AI¶
If you are stuck after trying for 20 minutes, use one of these prompts:
- "I am working on Error Safe Divider. I got this error: [paste error]. Can you explain what this error means without giving me the fix?"
- "I am trying to understand
try/except/else/finally. Can you explain what runs in each block with a simple example?" - "Can you explain the Python exception hierarchy and why catching
Exceptionis different from catchingBaseException?"
| ← Prev | Home | Next → |
|---|---|---|