1. Lighthouse

Level 5 · Boss: build the checkout calculator

The request

From Kevin Nguyen, Head of Engineering

“The checkout page is down and support is quoting totals by hand. Can you script the calculation so they can paste in a cart and get the exact total, coupon and shipping included?”

Your goal

Step 1 of 3. Set result to the cart subtotal as a float: lamp_price times lamp_qty plus pillow_price times pillow_qty, rounded to 2 decimals.

  • Store a number in resultnot done yet

Step 1 · 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.

    Step 2 · 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.

      Step 3 · 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

        Putting the basics together

        Real analysis code is rarely one clever line. It’s a short chain of plain steps, each easy to check: compute something, adjust it by a rule, present it. This boss level is exactly that chain, and it uses everything from the chapter: variables, arithmetic, rounding, string cleaning, conditions and f-strings.

        Step 1: the subtotal

        A cart line is price times quantity. A subtotal is the sum of the lines:

        subtotal = lamp_price * lamp_qty + pillow_price * pillow_qty

        Round once, at the end, to two decimals. Rounding each line first can make the total drift by a cent on larger carts.

        Step 2: a coupon typed by a human

        People type coupon codes in every possible way: pine10, PINE10, Pine10 . If you compare the raw text to "PINE10", most of them fail. Normalise first, then compare:

        code = coupon.strip().upper()
        if code == "PINE10":
            discounted = subtotal * 0.9
        else:
            discounted = subtotal

        A 10% discount means the customer pays 90%, so multiplying by 0.9 is the same as subtotal - subtotal * 0.10. Both are correct; pick the one that reads more clearly to you.

        Notice the else branch. Without it, discounted would not exist when the coupon is wrong, and the next line would fail with a NameError. Every path through your if should leave the variables you use later defined.

        Step 3: shipping and a readable total

        The shipping rule depends on the discounted amount, which is a common business choice: the customer has to spend $75 after discounts to earn free shipping. Once you have the final number, format it for people:

        total = discounted + shipping
        f"Total: ${total:.2f}"   # 'Total: $181.78'

        The :.2f format both rounds and pads, so 181.8 would print as 181.80. For support agents, that consistency matters: a total that sometimes shows one decimal looks like an error.

        Thinking like an analyst

        Before writing code for a rule like this, write the rule down in plain words and check the edge cases with the person who asked:

        • Does the coupon apply before or after shipping? (Here: before, to the items only.)
        • Is free shipping judged before or after the discount? (Here: after.)
        • What happens at exactly $75? (Here: free.)

        Most “bugs” in business calculations are not Python mistakes. They are rules that were never pinned down. Asking these questions early is a big part of the job.

        Common mistakes

        • Comparing the coupon without cleaning it, so " pine10 " never matches.
        • Applying shipping to the subtotal before the discount, which changes who gets free shipping.
        • Returning the number instead of the formatted text in the last step. The task asks for the string Total: $181.78, so result must be a str.