1. Lighthouse

Level 8 · Debug: charged shipping at exactly $75

The request

From Angela Rivera, Head of Customer Support

“A customer used a 25% coupon on a $100 cart, paid $75 for the goods, and we still charged her $6.95 shipping. Our site promises free shipping from $75. Can you find what's wrong in the checkout script?”

Your goal

Fix the code so result is the order total (a float rounded to 2): the discounted subtotal plus shipping. Shipping is 0.0 for business accounts or when the discounted subtotal is $75 or more, otherwise 6.95.

  • Store a number in resultnot done yet

Your code

Python

Loading the editor…

    ResultPress Run to see yours

    Mote checks your result

    Press Run. Each row that matches the goal becomes a firefly in your lantern.

    The lesson

    Find the case that broke

    Start from the complaint, not the code. The cart is 2 × $32.50 + $35.00 = $100.00, and a 25% coupon leaves the customer paying 100 * (1 - 25 / 100), which is exactly 75.0. The promise on the site is free shipping “from $75”, so this cart sits precisely on the boundary.

    discounted = 100.0 * (1 - 25 / 100)   # 75.0
    discounted > 75     # False  -> she pays 6.95
    discounted >= 75    # True   -> free shipping

    > means strictly greater. When both sides are equal it is False, so the script fell through to the else branch and charged $6.95. The total became $81.95 instead of $75.00.

    Translate the words into the operator

    Business rules use everyday words, and each one maps to a specific comparison:

    • “from $75”, “$75 or more”, “at least $75”: >= 75
    • “over $75”, “more than $75”: > 75
    • “under $50”, “below $50”: < 50
    • “up to $50”, “$50 or less”: <= 50

    Before you write a condition, underline the words in the policy and pick the operator from that list. Most boundary bugs come from writing > out of habit.

    Test the edges

    A good habit for any rule with a threshold is to try three values: one just below, one exactly on, and one just above. For this policy that means $74.99, $75.00 and $75.01. Only the first should pay shipping. The buggy script charges shipping on the first two, and you’d only catch it with the value exactly on the edge, which is why ordinary test carts didn’t reveal it.

    Notice also that the check uses the discounted subtotal. The order of operations matters in the business sense too: the coupon comes off first, then the shipping rule looks at what is left.

    Common mistakes

    • Comparing the original subtotal ($100) with 75. That would give free shipping to a $90 cart with a 25% coupon, which only costs $67.50.
    • Changing the threshold number instead of the operator, for example > 74.99.
    • Rounding discounted before the comparison and getting surprised by a float like 74.99999. Here the value is exactly 75.0, but if a calculation produces long decimals, round to cents first and then compare.

    Pick what may run on this site. You can change this any time from "Cookie settings" at the bottom of every page.