Total the cart in cents

Python · Python · beginner · greenfield

Adds the `cart_total_cents` helper for the checkout summary. Keeps all arithmetic in integer cents, applies the flat discount, and clamps at zero so an oversized promo code can't produce a negative total. Covered the empty-cart and big-discount cases by hand.

Requirements

Files touched

--- app/checkout/cart_total.py
+def cart_total_cents(lines, discount_cents=0):
+    """Total of a cart in integer cents, never below zero.
+
+    Each line has ``unit_cents`` (int >= 0) and ``quantity`` (int >= 0);
+    ``discount_cents`` is a flat discount applied to the whole cart.
+    """
+    subtotal = 0
+    for line in lines:
+        subtotal += line["unit_cents"] * line["quantity"]
+    return max(subtotal - discount_cents, 0)
+

Review this PR

Python practice