1. Lighthouse

Level 8 · Look up a fee in a rate table

The request

From Daniel Brooks, Head of Operations

“The carrier's rate card is by weight: up to 1 kg $6.95, up to 5 kg $9.95, up to 20 kg $14.95, up to 70 kg $49.00 freight. Anything heavier needs a manual quote. What do today's parcels cost?”

Your goal

Define fee_for(weight) that walks tiers in order and returns the fee of the first tier whose max weight is greater than or equal to weight, or None if no tier fits. Set result to a dictionary mapping each parcel id in parcels to fee_for(its weight), in the same order.

  • Store a dict 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

    Rules as data

    You could write the rate card as a chain of if/elif, but when the rules come as a table, keep them as a table. A list of (max_weight, fee) pairs, lightest first, holds the whole rate card:

    tiers = [(1, 6.95), (5, 9.95), (20, 14.95), (70, 49.00)]

    The function then needs no numbers of its own. When the carrier changes a price or adds a tier, you edit the list and the logic stays the same.

    Return as soon as you know

    The function walks the tiers in order and stops at the first one that fits. return ends the function immediately, which also ends the loop:

    def fee_for(weight):
        for max_weight, fee in tiers:
            if weight <= max_weight:
                return fee        # found it: leave now
        return None               # the loop finished without a match

    Walking through today’s parcels:

    • 0.8 kg: 0.8 ≤ 1, so $6.95 on the first check.
    • 1.0 kg: exactly on the limit. “Up to 1 kg” includes 1, hence <=, and the fee is $6.95.
    • 5.5 kg: too heavy for 1 and 5, fits 20, so $14.95.
    • 85 kg: fails all four tests, the loop ends, and the function returns None.

    The order of the list matters. Because the tiers go from light to heavy, the first match is also the cheapest tier that fits. If the list were shuffled, a 0.8 kg parcel could match the 70 kg tier first and be charged freight.

    None means “no answer”

    None is Python’s value for “nothing here”. It’s the right return value for the 85 kg parcel, because no fee on the card applies. Returning 0 would look like free shipping, which is a costly mistake. In the result dictionary the parcel shows up as None, so whoever reads it knows to ask for a quote. Any function that ends without reaching a return also gives back None, so the last line is mostly there to make the intent obvious.

    Common mistakes

    • Writing else: return None inside the loop. The function stops after checking only the first tier, so only parcels up to 1 kg get a fee.
    • Using < instead of <=, which charges a 1.0 kg parcel the 5 kg rate.
    • Returning 0 for parcels that fit no tier.

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