Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
  • Loading branch information
nickzerjeski and Copilot authored Apr 14, 2026
commit d5e1fb057c8e6abcb32d3cc64608dc82b494e6e3
37 changes: 33 additions & 4 deletions other/grocery_store_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ class GroceryStoreCart:
...
ValueError: quantity must be positive

>>> empty_cart = GroceryStoreCart({"apple": 1.5})
>>> empty_cart.remove_item("apple")
Traceback (most recent call last):
...
KeyError: "'apple' is not present in the cart"

>>> GroceryStoreCart({})
Traceback (most recent call last):
...
ValueError: price_catalog cannot be empty

>>> cart.add_item("bread")
Traceback (most recent call last):
...
KeyError: "'bread' is not in the catalog"

>>> cart.add_item("apple", 0)
Traceback (most recent call last):
...
ValueError: quantity must be positive

>>> cart.remove_item("milk", 0)
Traceback (most recent call last):
...
ValueError: quantity must be positive

>>> empty_cart = GroceryStoreCart({"apple": 1.5})
>>> empty_cart.remove_item("apple")
Traceback (most recent call last):
Expand Down Expand Up @@ -66,14 +92,17 @@ def remove_item(self, item: str, quantity: int = 1) -> None:
raise KeyError(msg)
if quantity > current:
raise ValueError("quantity exceeds amount present in the cart")
if (remaining := current - quantity) > 0:
self.quantities[item] = remaining
else:
if quantity > current:
raise ValueError("quantity exceeds amount present in the cart")
if quantity == current:
self.quantities.pop(item, None)
else:
self.quantities[item] = current - quantity

def total_price(self) -> float:
return sum(
self.price_catalog[item] * qty for item, qty in self.quantities.items()
self.price_catalog[item] * qty
for item, qty in self.quantities.items()
)


Expand Down