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, setshippingto0; otherwise set it toshipping_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 beSubtotal: $120,Shipping: $0,Total: $120. - Join the three lines with a newline
"\n"into one stringresult. Just store the text inresult; 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
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
