1. Lighthouse

Level 1 · Compute an order total

The request

From Megan Collins, Head of Sales

“A customer on the phone is buying two of our table lamps and wants the total before she pays. Can you work it out, shipping included?”

Your goal

Set result to the order total as a float: lamp_price times quantity, plus shipping_fee, rounded to 2 decimal places with round().

  • 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

    Variables are labelled boxes

    A variable is a name that points at a value. You create one with a single equals sign:

    lamp_price = 49.99
    quantity = 2

    Now lamp_price means 49.99 anywhere later in your code. Names should say what the value is. lamp_price is much easier to read next month than x or p1. Python names use lowercase words joined by underscores, a style called snake_case.

    The = sign means “store this value under this name”, not “is equal to”. You can overwrite a variable at any time, and the new value replaces the old one:

    quantity = 2
    quantity = 3   # quantity is now 3

    Arithmetic

    Python does the arithmetic you already know:

    operator meaning example result
    + add 49.99 + 7.50 57.49
    - subtract 120 - 15 105
    * multiply 49.99 * 2 99.98
    / divide 30 / 4 7.5
    ** power 1.05 ** 2 1.1025

    Multiplication and division happen before addition and subtraction, just like on paper. Use brackets when you want a different order, or simply when they make your intent clearer:

    subtotal = 49.99 * 2 + 34.00 * 3       # 201.98
    with_tax = (49.99 * 2) * 1.08          # brackets for clarity

    Numbers have types

    Whole numbers like 2 are integers (int). Numbers with a decimal point like 49.99 are floats. Mixing them is fine: 49.99 * 2 gives the float 99.98. You can check any value’s type with type():

    type(2)       # <class 'int'>
    type(49.99)   # <class 'float'>

    Rounding money

    Floats are stored in binary, so some decimal values cannot be represented exactly. That’s why 0.1 + 0.2 prints 0.30000000000000004. It’s not a bug in your code; every language that uses standard floats behaves this way. For money, round the final figure to cents:

    round(0.1 + 0.2, 2)        # 0.3
    round(49.99 * 3, 2)        # 149.97

    round(value, 2) keeps two decimal places. Round at the end of a calculation, not at every step, so small rounding errors don’t add up.

    How these levels are checked

    Every Python level asks you to store your answer in a variable named result. When you run your code, the game compares your result with the expected value. Any correct approach passes, so result = round(lamp_price * quantity + shipping_fee, 2) and a version that builds the total in two steps are both fine:

    items_total = lamp_price * quantity
    result = round(items_total + shipping_fee, 2)

    Common mistakes

    • Typing the numbers straight into the formula instead of using the variables. It works today, but the point of variables is that the same code still works when the price changes.
    • Forgetting to assign: writing round(...) on its own line computes the value and throws it away. It needs result = in front.
    • Using == when you mean =. Two equals signs compare values; one stores a value.