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
- Create
subtotal.price * quantity, which is 36. It is the cost before any discount or tax. - Create
after_discount.subtotal - discount, which is 31. - 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. - Create
total.after_discount + tax, which is 33. This is what the customer pays. - Build
line1toline4. Each row is a label with its dollar sign followed by one number turned into text withstr():Subtotal,Discount,TaxandTotal, in that order. - 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 / 100is2.48, and the receipt would showTax: $2.48and a fractional total. Floor division gives the whole number 2. - The order of the expression.
after_discount * tax_rate // 100is 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 raisesTypeError: can only concatenate str (not "int") to str. itemgoes 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
subtotalinstead ofafter_discountin 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 = 0and 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_discountis -14, and//rounds toward negative infinity:-14 * 8 // 100is -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_rateas 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.
