Problem overview
Version 3 of the shopping cart adds a shipping charge that can disappear. The starting variables are subtotal (75), shipping_cost (10) and free_shipping_threshold (100). A cart that reaches the threshold ships free, and any other cart pays the regular shipping cost. The program stores a three-line receipt in the string result, so nothing needs printing.
With the starting values, 75 is below 100, so shipping costs 10 and the total is 85:
Subtotal: $75
Shipping: $10
Total: $85
All three lines always appear. When shipping is free, its line shows $0 instead of disappearing.
Approach: decide the shipping, then build the text
The shipping amount depends on a yes-or-no question, so it is a job for if/else. The lesson answers the question first and stores the outcome in a variable named shipping. Everything after that, the total and the three rows, uses shipping and never asks the question again.
Keeping shipping_cost and shipping apart matters. shipping_cost is the price list: what shipping costs when it is charged. shipping is what this particular cart pays, which is either that price or 0.
Step by step
- Write the
if. The condition issubtotal >= free_shipping_threshold. Inside it,shippingbecomes 0. The comparison is "greater than or equal" because a cart that lands exactly on the threshold ships free. - Write the
else. It lines up with theifand setsshippingtoshipping_cost. Exactly one of the two branches runs, soshippingalways has a value afterwards. - Create
total. It issubtotal + shipping. For a free-shipping cart the total equals the subtotal. - Build
line1,line2andline3. Each row is a label that ends with a dollar sign, followed by one number turned into text withstr():Subtotal,ShippingandTotal, in that order. - Join the rows into
result. Three rows need two newline characters, one between each pair of neighbours. Nothing follows the last row.
The solution in Python
subtotal = 75
shipping_cost = 10
free_shipping_threshold = 100
if subtotal >= free_shipping_threshold:
shipping = 0
else:
shipping = shipping_cost
total = subtotal + shipping
line1 = "Subtotal: $" + str(subtotal)
line2 = "Shipping: $" + str(shipping)
line3 = "Total: $" + str(total)
result = line1 + "\n" + line2 + "\n" + line3
Details worth knowing:
- Only the branch that matches runs. The body of
ifand the body ofelseare indented blocks. Python uses the indentation to decide which lines belong to which branch, so the four spaces are part of the syntax. - Why
str()is needed. The labels are strings and the amounts are integers. Writing"Total: $" + 80raisesTypeError: can only concatenate str (not "int") to str. The dollar sign inside the string is an ordinary character. - The newline.
"\n"is a single character that ends a line. Here is the result string for a cart of 75 as Python shows it, with the newlines visible:'Subtotal: $75\nShipping: $10\nTotal: $85'. - Whole numbers. The amounts are integers, so nothing is rounded and no decimal point appears in the output.
Edge cases and common mistakes
- A cart exactly on the threshold. With a subtotal of 100 the result is
Subtotal: $100,Shipping: $0,Total: $100. Writing>instead of>=would charge this cart 10. - One below the threshold. With a subtotal of 99 the lines are
Subtotal: $99,Shipping: $10andTotal: $109: the shipping brings the total above the threshold, and that is fine. - Dropping the shipping line. When shipping is free, the receipt still has three lines. Skipping the middle row would break the format.
- Computing
totalbefore theif/else.shippingwould not exist yet, and Python would raise aNameError. - Putting
totalinside a branch. If it were indented under theelse, free-shipping carts would never get a total. - Forgetting a newline. Two rows would end up on the same line.
Testing
subtotal = 75gives 75, 10 and 85.subtotal = 120gives 120, 0 and 120.subtotal = 100gives 100, 0 and 100.subtotal = 99gives 99, 10 and 109.
