Skip to content

Commit d5e1fb0

Browse files
Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent eedcb14 commit d5e1fb0

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

other/grocery_store_cart.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ class GroceryStoreCart:
3636
...
3737
ValueError: quantity must be positive
3838
39+
>>> empty_cart = GroceryStoreCart({"apple": 1.5})
40+
>>> empty_cart.remove_item("apple")
41+
Traceback (most recent call last):
42+
...
43+
KeyError: "'apple' is not present in the cart"
44+
45+
>>> GroceryStoreCart({})
46+
Traceback (most recent call last):
47+
...
48+
ValueError: price_catalog cannot be empty
49+
50+
>>> cart.add_item("bread")
51+
Traceback (most recent call last):
52+
...
53+
KeyError: "'bread' is not in the catalog"
54+
55+
>>> cart.add_item("apple", 0)
56+
Traceback (most recent call last):
57+
...
58+
ValueError: quantity must be positive
59+
60+
>>> cart.remove_item("milk", 0)
61+
Traceback (most recent call last):
62+
...
63+
ValueError: quantity must be positive
64+
3965
>>> empty_cart = GroceryStoreCart({"apple": 1.5})
4066
>>> empty_cart.remove_item("apple")
4167
Traceback (most recent call last):
@@ -66,14 +92,17 @@ def remove_item(self, item: str, quantity: int = 1) -> None:
6692
raise KeyError(msg)
6793
if quantity > current:
6894
raise ValueError("quantity exceeds amount present in the cart")
69-
if (remaining := current - quantity) > 0:
70-
self.quantities[item] = remaining
71-
else:
95+
if quantity > current:
96+
raise ValueError("quantity exceeds amount present in the cart")
97+
if quantity == current:
7298
self.quantities.pop(item, None)
99+
else:
100+
self.quantities[item] = current - quantity
73101

74102
def total_price(self) -> float:
75103
return sum(
76-
self.price_catalog[item] * qty for item, qty in self.quantities.items()
104+
self.price_catalog[item] * qty
105+
for item, qty in self.quantities.items()
77106
)
78107

79108

0 commit comments

Comments
 (0)