Start learning free

LESSON 187 OF 287 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V3 in Python: Free Shipping with if/else

Practice Read the full explanation

Use an if/else to decide whether to charge shipping based on a free-shipping threshold.

You start with:

subtotal = 75
shipping_cost = 10
free_shipping_threshold = 100

Build:

Subtotal: $75
Shipping: $10
Total: $85

Rules:

  • If subtotal >= free_shipping_threshold, set shipping to 0; otherwise set it to shipping_cost.
  • total = subtotal + shipping.
  • Always include all three lines (Subtotal, Shipping, Total). When shipping is free, the line shows $0: for a subtotal of 120 the lines would be Subtotal: $120, Shipping: $0, Total: $120.
  • Join the three lines with a newline "\n" into one string result. Just store the text in result; you do not need to print it.

Decide shipping in an if/else first, then build the three lines.

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

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

STEP 1 OF 7
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V3 in Python, explained

    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

    1. Write the if. The condition is subtotal >= free_shipping_threshold. Inside it, shipping becomes 0. The comparison is "greater than or equal" because a cart that lands exactly on the threshold ships free.
    2. Write the else. It lines up with the if and sets shipping to shipping_cost. Exactly one of the two branches runs, so shipping always has a value afterwards.
    3. Create total. It is subtotal + shipping. For a free-shipping cart the total equals the subtotal.
    4. Build line1, line2 and line3. Each row is a label that ends with a dollar sign, followed by one number turned into text with str(): Subtotal, Shipping and Total, in that order.
    5. 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 if and the body of else are 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: $" + 80 raises TypeError: 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: $10 and Total: $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 total before the if/else. shipping would not exist yet, and Python would raise a NameError.
    • Putting total inside a branch. If it were indented under the else, free-shipping carts would never get a total.
    • Forgetting a newline. Two rows would end up on the same line.

    Testing

    • subtotal = 75 gives 75, 10 and 85.
    • subtotal = 120 gives 120, 0 and 120.
    • subtotal = 100 gives 100, 0 and 100.
    • subtotal = 99 gives 99, 10 and 109.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free