Part 04 / Topic 11 of 11
Putting it together: shopping cart
prices = {"rice": 60, "milk": 30, "soap": 45}
categories = {"rice": "grocery", "milk": "dairy", "soap": "personal care"}
cart = ["rice", "milk", "rice", "soap"]
total = sum(prices[item] for item in cart) # generator expression (see section 7)
print(f"Total: ₹{total}") # Total: ₹195
print(f"Rice count: {cart.count('rice')}") # Rice count: 2
unique_categories = {categories[item] for item in cart}
print(sorted(unique_categories)) # ['dairy', 'grocery', 'personal care']
See the full version in examples/06_shopping_cart.py.
Done here? The practice set for Part 4 — 8 exercises with solutions — is on the part page.