Editor

Test a Cart Total with Coupons

Write the tests · Advanced · about 15 min

cart_total(prices, coupon=None) adds up a list of prices and applies a coupon code. "SAVE10" takes 10% off. "FLAT5" takes 5 off, but only when the prices add up to 20 or more. Codes are not case-sensitive, so "save10" works too, and no coupon or any other code changes nothing. The result is rounded to 2 decimal places.

Write test_ functions with assert that pass on a correct version and fail on broken ones.

Examples

cart_total([10, 15])              # 25
cart_total([30, 20], "SAVE10")    # 45.0
cart_total([15, 10], "FLAT5")     # 20
def test_no_coupon():
assert cart_total([10, 15]) == 25
The solution opens after your first Check.