A beginner price calculator should have an explicit rounding rule. Python’s Decimal type lets you represent decimal inputs directly and choose how to round a result. This exercise uses fictional prices to practise that decision before you connect a calculator to a web form.
Start with a clear input contract
Use three inputs: a unit price supplied as text, a positive whole-number quantity and an agreed rounding rule. For this exercise, the price is “19.95” and the quantity is three. The expected total is 59.85. No tax, discount, delivery charge or currency conversion is included.
Keep that scope visible in your README. A reviewer should not have to guess whether the result is a subtotal or a final amount. In a real application, the business owner must define the additional rules.
A small runnable example
from decimal import Decimal, ROUND_HALF_UP
unit_price = Decimal("19.95")
quantity = 3
subtotal = unit_price * quantity
display_total = subtotal.quantize(
Decimal("0.01"), rounding=ROUND_HALF_UP
)
print(display_total) # 59.85
assert display_total == Decimal("59.85")Construct Decimal from the original text rather than first converting the input to a float. Python’s decimal documentation explains that construction from a float preserves that float’s exact binary value, which can introduce unexpected digits. Decimal arithmetic still has a precision context; it is not a promise of unlimited precision.
Choose where rounding happens
Consider two fictional line amounts of 0.005 each. Under the HALF_UP rule used here, rounding each line to two places gives 0.01 plus 0.01, or 0.02. Adding first and rounding the total gives 0.010, displayed as 0.01. Neither workflow should be chosen accidentally.
This is a software-design exercise, not a recommendation for a tax or accounting policy. Record whether your project rounds individual lines or the final sum, and use the same policy in the interface, database and tests. Different currencies and applications can require different scales.
Write a test table before expanding the code
| Input | Expected behaviour in this exercise |
|---|---|
| “19.95”, quantity 3 | Return 59.85 |
| “0.00”, quantity 1 | Return 0.00 if zero-price items are allowed |
| “abc” | Show an invalid-price error |
| Quantity -1 or 1.5 | Reject: this exercise requires a positive integer |
| “NaN” or “Infinity” | Reject: a price must be finite |
The short example above demonstrates arithmetic only; it does not implement these validation cases. Your next task is to add them. Catch invalid decimal input, check that the result is finite, enforce your allowed range and validate quantity separately. A value being parseable does not make it an acceptable price.
Turn it into a portfolio feature
- Create a function with a documented input and output contract.
- Add automated tests for the table, including boundary values.
- Build a simple form that explains errors beside the relevant field.
- Validate on the server as well as in the browser.
- Show the rounding rule and scope beside a sample calculation.
Use fictional products and local practice data. Avoid accepting real payments while experimenting. A useful demonstration includes one successful calculation and one rejected input, followed by an explanation of why the rejection is correct.
Review AI-generated code with the test cases
If an AI assistant supplies a calculator function, check whether it converts text through float, silently accepts negative quantities or treats a failed parse as zero. Run your own examples and ask it to explain the rounding boundary. A plausible explanation is not a passing test.
Explore Python web development
Continue with the Django project checklist, or review ScholarLogic’s Python Full Stack course in Bangalore. For current syllabus, fees and batch availability, contact the counselling team. This practice exercise is not a job guarantee.
Prepared with AI assistance for ScholarLogic. Prices and scenarios are fictional learning examples.
