Model invoice lines with @dataclass and compute totals with decimal.Decimal (never floats for money):
{"sku", "qty", "unit_price"} where unit_price is a string like "19.99".discount_pct percent of the subtotal, rounded half-up.tax_pct percent of (subtotal − discount), rounded half-up. Total = subtotal − discount + tax.Return a dictionary of strings with two decimals: {"lines": [...line totals], "subtotal", "discount", "tax", "total"}.
Implement build_invoice. Input: lines: list[dict], discount_pct: str, tax_pct: str. Returns: dict.
from dataclasses import dataclass
from decimal import Decimal
@dataclass
class Line:
sku: str
qty: int
unit_price: Decimal
def build_invoice(lines: list[dict], discount_pct: str, tax_pct: str) -> dict:
total = sum(l["qty"] * float(l["unit_price"]) for l in lines)
return {"lines": [], "subtotal": str(total), "discount": "0.00", "tax": "0.00", "total": str(total)}
Run executes the public tests. Submit grades against public and hidden tests.
Model invoice lines with @dataclass and compute totals with decimal.Decimal (never floats for money):
{"sku", "qty", "unit_price"} where unit_price is a string like "19.99".discount_pct percent of the subtotal, rounded half-up.tax_pct percent of (subtotal − discount), rounded half-up. Total = subtotal − discount + tax.Return a dictionary of strings with two decimals: {"lines": [...line totals], "subtotal", "discount", "tax", "total"}.
Implement build_invoice. Input: lines: list[dict], discount_pct: str, tax_pct: str. Returns: dict.
from dataclasses import dataclass
from decimal import Decimal
@dataclass
class Line:
sku: str
qty: int
unit_price: Decimal
def build_invoice(lines: list[dict], discount_pct: str, tax_pct: str) -> dict:
total = sum(l["qty"] * float(l["unit_price"]) for l in lines)
return {"lines": [], "subtotal": str(total), "discount": "0.00", "tax": "0.00", "total": str(total)}
Run executes the public tests. Submit grades against public and hidden tests.