Level 1 / Project 03 - Unit Price Calculator¶
Home: README
Try in Browser: Run this exercise online — no installation needed!
Before You Start¶
Recall these prerequisites before diving in:
- Can you open and read a CSV file using Python's csv module?
- Can you sort a list using sorted() with a key argument?
Estimated time: 25 minutes
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | — | Browser |
Focus¶
- math accuracy and formatting
Why this project exists¶
Compare products by computing price-per-unit from CSV data, rank them from best to worst deal, and find the best value. You will learn CSV parsing with csv.DictReader, float arithmetic, and sorting with key functions.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-1/03-unit-price-calculator
python project.py --input data/sample_input.txt
pytest -q
Expected terminal output¶
=== Unit Price Comparison ===
Rice 10lb Bag $15.49 / 10 lb => $1.55/lb
Rice 5lb Bag $8.99 / 5 lb => $1.80/lb
Best deal: Rice 10lb Bag at $1.55/lb
5 passed
Expected artifacts¶
data/output.json- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add a
--categoryfilter flag that shows only products matching a category column. - Add a "savings" column showing how much you save vs the most expensive option.
- Re-run script and tests.
Break it (required) — Core¶
- Add a CSV row with quantity
0-- doescalculate_unit_price()crash with division by zero? - Add a row with a negative price -- does the calculator accept it or reject it?
- Add a row with missing columns -- does
parse_product_row()handle it gracefully?
Fix it (required) — Core¶
- Ensure
calculate_unit_price()raisesValueErrorfor zero or negative quantities. - Validate that prices are non-negative in
parse_product_row(). - Add a test for the zero-quantity edge case.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- Why does
csv.DictReadermap columns by header name instead of by index? - What does
round(price / quantity, 2)do and why round to 2 decimal places? - Why does
find_best_deal()usemin()with akeyfunction instead of a manual loop? - Where would unit price calculations appear in real software (grocery apps, procurement systems)?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
Stuck? Ask AI¶
If you are stuck after trying for 20 minutes, use one of these prompts:
- "I am working on Unit Price Calculator. I got this error: [paste error]. Can you explain what this error means without giving me the fix?"
- "I am trying to sort a list of dictionaries by one of the values. Can you show me how
sorted()with akeyfunction works, using a different example?" - "Can you explain
csv.DictReaderwith a simple example that is not about prices?"
| ← Prev | Home | Next → |
|---|---|---|