You get a finished piece of work that has already been tested against your own standard, rather than a first draft you must police. It works because the goal, effort level and self-check are all stated up front, so the AI knows what done looks like and how hard to try.
Worked in our testIt restated the goal in two sentences, covered rounding, missing discounts and negatives up front, and delivered one commented function with five correct pytest tests. It then gave a Checked section tracing each test, two invented edge cases, no fixes needed and an honest note that nothing was run.
The prompt
Act as a careful, practical colleague who finishes work properly and checks it before handing it over.
Task: Complete this job: {task_goal}. The finished result should look like this: {finished_result}. Effort level: {effort_level}.
Steps:
1. Restate the goal and the finished result in two sentences, so we agree on what "done" means.
2. Match your effort to the level I gave. If it is high, think through options and edge cases before you start. If it is low, give a quick answer.
3. Do the work.
4. Check your own work using this method: {check_method}. Note anything that fails and fix it.
5. Repeat the check until it passes, or say clearly what still does not.
Output: First the finished work. Then a short "Checked" section listing each check you ran, with a pass or fail result, and any fixes made. Plain British English, no padding.
Change the parts in curly braces. Use it for any task with a checkable outcome, such as code, spreadsheets formulas, reports with figures, or emails that must hit set points.
What happened when we ran it
We sent
Act as a careful, practical colleague who finishes work properly and checks it before handing it over.
Task: Complete this job: Write a Python function that takes a list of customer orders (each with a price in pence, a quantity and an optional discount percentage) and returns the total order value in pounds, rounded to two decimal places. The finished result should look like this: A single, commented function called order_total, plus five unit tests in pytest format covering an empty list, a single item, a discount, a zero quantity and a large order. Effort level: High. Think through rounding errors, missing discount values and negative numbers before writing any code.
Steps:
1. Restate the goal and the finished result in two sentences, so we agree on what "done" means.
2. Match your effort to the level I gave. If it is high, think through options and edge cases before you start. If it is low, give a quick answer.
3. Do the work.
4. Check your own work using this method: Mentally run each pytest test against your function, line by line, and confirm the expected value matches the result. Also test one case you invented yourself that could break it. Note anything that fails and fix it.
5. Repeat the check until it passes, or say clearly what still does not.
Output: First the finished work. Then a short "Checked" section listing each check you ran, with a pass or fail result, and any fixes made. Plain British English, no padding.
The model replied
**Goal and definition of done.** Write one commented function, `order_total`, that turns a list of orders (price in pence, quantity, optional discount %) into a total in pounds, rounded to two decimal places. It comes with five pytest tests (empty list, single item, discount, zero quantity, large order), each checked by hand against the code.
**Decisions made up front (effort: high)**
- **Rounding errors:** floats are avoided. All sums use `Decimal`, and rounding happens once, on the final total, using round-half-up. Per-line rounding would drift on big orders.
- **Missing discount:** an absent key or `None` means 0%.
- **Negative or invalid values:** a negative price or quantity, a discount outside 0–100, a non-whole quantity, a missing key or a non-numeric value raises `ValueError`. Silently accepting them would hide bad data, and a negative price would reduce the bill.
## order_total.py
```python
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
def order_total(orders):
"""Return the total value of a list of orders, in pounds, to 2 d.p.
Each order is a dict with:
"price": unit price in pence (>= 0)
"quantity": whole number of units (>= 0)
"discount": optional percentage off the line, 0-100.
A missing key or None means no discount.
Money is handled with Decimal to avoid float errors. Lines are summed
unrounded and the total is rounded once at the end (half up).
Raises ValueError for missing keys, non-numeric values, negative
prices or quantities, non-integer quantities, or discounts outside 0-100.
"""
total_pence = Decimal("0")
for index, order in enumerate(orders):
# Pull out the required fields, with a clear error if one is absent.
try:
raw_price = order["price"]
quantity = order["quantity"]
except KeyError as exc:
raise ValueError(f"Order {index} is missing {exc}") from None
# Treat a missing or None discount as 0%.
raw_discount = order.get("discount")
if raw_discount is None:
raw_discount = 0
# Convert via str() so a float like 19.99 is not stored as 19.989999...
try:
price = Decimal(str(raw_price))
discount = Decimal(str(raw_discount))
except InvalidOperation:
raise ValueError(f"Order {index} has a non-numeric value") from None
# Validate. bool is excluded because True would otherwise count as 1.
if not price.is_finite() or price < 0:
raise ValueError(f"Order {index}: price must be a number >= 0")
if isinstance(quantity, bool) or not isinstance(quantity, int) or quantity < 0:
raise ValueError(f"Order {index}: quantity must be a whole number >= 0")
if not discount.is_finite() or not (0 <= discount <= 100):
raise ValueError(f"Order {index}: discount must be between 0 and 100")
# Line value in pence: price x quantity x (100 - discount) / 100.
total_pence += price * quantity * (Decimal(100) - discount) / Decimal(100)
# Convert pence to pounds and round once, half up.
total_pounds = (total_pence / Decimal(100)).quantize(
Decimal("0.01"), rounding=ROUND_HALF_UP
)
return float(total_pounds)
```
## test_order_total.py
```python
from order_total import order_total
def test_empty_list():
assert order_total([]) == 0.0
def test_single_item():
# 1 x 1999p = £19.99
assert order_total([{"price": 1999, "quantity": 1}]) == 19.99
def test_discount():
orders = [
{"price": 1000, "quantity": 2, "discount": 10}, # 2000p less 10% = 1800p
{"price": 500, "quantity": 1, "discount": None}, # no discount = 500p
]
assert order_total(orders) == 23.0
def test_zero_quantity():
assert order_total([{"price": 999, "quantity": 0, "discount": 50}]) == 0.0
def test_large_order():
# Each line: 1999p x 3 = 5997p, less 15% = 5097.45p.
# 10,000 lines = 50,974,500p = £509,745.00
orders = [{"price": 1999, "quantity": 3, "discount": 15}] * 10_000
assert order_total(orders) == 509745.0
```
## Checked
I traced everything by hand. I did not execute the code, so run `pytest` once to confirm.
- **test_empty_list: pass.** The loop never runs, so the total is 0. 0/100 quantizes to 0.00, and `float` gives 0.0.
- **test_single_item: pass.** 1999 × 1 × 100 / 100 = 1999p. 1999/100 = 19.99, and `float(Decimal("19.99")) == 19.99`.
- **test_discount: pass.** Line 1 is 1000 × 2 × 90 / 100 = 1800. Line 2 has a `None` discount, so it becomes 0, giving 500. The total is 2300p, so £23.00 and `23.0`.
- **test_zero_quantity: pass.** 999 × 0 × 50 / 100 = 0, so the result is 0.0. Validation accepts quantity 0.
- **test_large_order: pass.** 1999 × 3 = 5997. 5997 × 85 / 100 = 5097.45 exactly in Decimal. × 10,000 = 50,974,500p, so £509,745.00. Floats are not involved, so there is no drift.
- **My own edge case, half-penny rounding: pass.** `{"price": 1, "quantity": 1, "discount": 50}` gives 0.5p, then £0.005, which rounds half up to 0.01. Plain float `round()` could give 0.0 or 0.01 depending on representation.
- **My own edge case, invalid input: pass.**
- A price of -5 raises `ValueError`.
- A discount of 150 raises `ValueError`.
- A missing `"quantity"` key raises `ValueError` with the order index.
- A quantity of `2.5` raises `ValueError`.
- **Fixes made:** none needed on the final trace. While designing I added `str()` conversion before `Decimal` (so float inputs like 19.99 stay exact) and the `bool` and non-finite checks.
**Still open:** the brief didn't say whether to round per line or on the total. I chose the total. If your invoices round each line to the nearest penny, that is a one-line change, but totals can then differ by a penny or two.
Run on Claude Sonnet 5.5, 26 Oct 2026. Automatic check: 5 out of 5.