Shopping basket with a discount code
Mediumclasslist attributemethodsround· 20 XP
An online shop's basket holds items and their prices, and a discount code takes a percentage off the total.
Write a class Basket with:
add(name, price, qty=1)that adds an itemsubtotal()that returns the sum of price × quantity for all itemsapply_code(code)that stores a discount:"SAVE10"is 10% off and"SAVE25"is 25% off; any other code raisesValueError("unknown code")total()that returns the subtotal after the discount, rounded to 2 decimals
How it's checked
Your code runs, then the check below runs and its printed output is compared with ours.
- Two items at 4.00 and one at 10.00 with
"SAVE10"give a total of16.2
Hints, the later questions and the answer come with Pro.
class Basket:
def __init__(self):
...