Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ShoppingCart:
- def __init__(self):
- self.total = 0
- self.items = dict()
- #self.items = {}
- def add_item(self, item_name, quantity, price):
- self.total += quantity*price
- if item_name not in self.items:
- self.items[item_name] = quantity
- else:
- self.items[item_name] += quantity
- # ---- test ----
- cart = ShoppingCart()
- cart.add_item('banana', 3, 4)
- cart.add_item('banana', 5, 4) # add againg the same item
- cart.add_item('orange', 2, 3)
- print('total:', cart.total)
- print('items:', cart.items)
- # total: 38
- # items: {'orange': 2, 'banana': 8}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement