Start learning free

LESSON 160 OF 410 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V3 - Free Shipping in Python

Practice

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

    Shopping Cart V3 in Python

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free