Start learning free

LESSON 187 OF 673 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in Python: Tax and Discount with Integer Math

Practice Read the full explanation

Add a discount and sales tax to a single-item purchase using integer math.

You start with:

item = "Coffee Mug"
price = 12
quantity = 3
tax_rate = 8
discount = 5

Build:

Subtotal: $36
Discount: $5
Tax: $2
Total: $33

Rules:

  • subtotal = price * quantity.
  • after_discount = subtotal - discount (the discount is a flat amount taken off before tax).
  • tax = after_discount * tax_rate // 100 (integer division, which rounds down). The tax is charged on the discounted amount.
  • total = after_discount + tax.
  • Build four lines (Subtotal, Discount, Tax, Total) and join them with a newline "\n" into one string result. Just store the text in result; you do not need to print it.

Compute the four numbers (subtotal, after_discount, tax, total) before formatting.

Use str() to convert each number while building the lines.

Use // so the tax is a whole number rounded down.

item = "Coffee Mug"
price = 12
quantity = 3
tax_rate = 8
discount = 5

subtotal = price * quantity
after_discount = subtotal - discount
tax = after_discount * tax_rate // 100
total = after_discount + tax
line1 = "Subtotal: $" + str(subtotal)
line2 = "Discount: $" + str(discount)
line3 = "Tax: $" + str(tax)
line4 = "Total: $" + str(total)
result = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4

STEP 1 OF 9
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in Python, explained

    Problem overview

    Version 2 of the shopping cart builds an itemised receipt. The starting variables are item, price, quantity, tax_rate (a percentage) and discount (a flat amount). The program computes four figures and stores a four-line receipt in the string result, without printing it.

    The rules fix the order of operations. The subtotal is price times quantity. The discount is subtracted from the subtotal first. The tax is then charged on the discounted amount, using integer division, which rounds down. The total is the discounted amount plus the tax. With a mug at 12, quantity 3, tax rate 8 and discount 5, the receipt is:

    Subtotal: $36
    Discount: $5
    Tax: $2
    Total: $33

    Approach: one variable per stage

    Each figure depends on the one before it, so the lesson computes them in that order and gives each its own name: subtotal, after_discount, tax, total. The names tell the story of the calculation, and the same variables are then reused when building the text.

    The order matters because tax is a percentage of whatever amount it is applied to. The discount comes off first, so the tax is calculated on 31 rather than on 36. A different order can change the result: with a discount of 10 and a rate of 10, the tax on the discounted 26 is 2, while the tax on the full 36 would be 3. The lesson's tests follow the stated rule.

    Money is handled in whole numbers here. The tax formula multiplies by the rate first and then divides by 100 with //. Multiplying first keeps as much precision as possible before the division rounds down.

    Step by step

    1. Create subtotal. price * quantity, which is 36. It is the cost before any discount or tax.
    2. Create after_discount. subtotal - discount, which is 31.
    3. Create tax. after_discount * tax_rate // 100. For 31 and a rate of 8 this is 248 // 100, which is 2. The // operator drops the fractional part, so the tax stays whole.
    4. Create total. after_discount + tax, which is 33. This is what the customer pays.
    5. Build line1 to line4. Each row is a label with its dollar sign followed by one number turned into text with str(): Subtotal, Discount, Tax and Total, in that order.
    6. Join the rows into result. Four rows need three newline characters, one between each pair of neighbours. There is no newline after the last row.

    The solution in Python

    item = "Coffee Mug"
    price = 12
    quantity = 3
    tax_rate = 8
    discount = 5
    
    subtotal = price * quantity
    after_discount = subtotal - discount
    tax = after_discount * tax_rate // 100
    total = after_discount + tax
    line1 = "Subtotal: $" + str(subtotal)
    line2 = "Discount: $" + str(discount)
    line3 = "Tax: $" + str(tax)
    line4 = "Total: $" + str(total)
    result = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
    

    Some details worth knowing:

    • Why // and not /. Ordinary division returns a float in Python. 31 * 8 / 100 is 2.48, and the receipt would show Tax: $2.48 and a fractional total. Floor division gives the whole number 2.
    • The order of the expression. after_discount * tax_rate // 100 is evaluated from left to right, because * and // have the same precedence. That is the multiply-then-divide order the rule asks for.
    • Every number is converted. The labels are strings and the figures are integers, so each row needs str(). Without it, Python raises TypeError: can only concatenate str (not "int") to str.
    • item goes unused. The receipt in this version shows only the four amounts.
    • Rounding goes down. Rounding is applied to the tax only, and it favours the customer. A tax of 2.48 becomes 2, and the shop never charges the missing 0.48.

    Edge cases and common mistakes

    • Taxing the subtotal. Using subtotal instead of after_discount in the tax line overcharges the customer. With a subtotal of 36, a discount of 10 and a rate of 10, the right tax is 2 and the wrong one is 3.
    • Subtracting the discount at the end. That applies the discount after the tax, which is a different rule and a different total.
    • Zero discount. With discount = 0 and a rate of 10, the receipt reads 36, 0, 3 and 39: the tax is 3.6 rounded down to 3.
    • A discount larger than the subtotal. With a discount of 50, after_discount is -14, and // rounds toward negative infinity: -14 * 8 // 100 is -2, not -1. The code has no guard, and the lesson's data never reaches this case.
    • Forgetting a newline. Two rows would run together on one line. Count three separators for four rows.
    • Treating tax_rate as a fraction. The rate is 8, meaning 8 percent, so the division by 100 is part of the formula.

    Testing

    • Price 12, quantity 3, tax rate 8, discount 5 gives 36, 5, 2 and 33.
    • Price 10, quantity 2, tax rate 10, discount 0 gives subtotal 20, discount 0, tax 2 and total 22.
    • Price 12, quantity 3, tax rate 0, discount 6 gives 36, 6, 0 and 30.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free