From ec1c3c54b4bed40c672b47b33943a7c7be2c50de Mon Sep 17 00:00:00 2001 From: Oleksandr Merlenko Date: Thu, 31 Oct 2024 18:08:06 +0100 Subject: [PATCH] Solution --- app/customer.py | 10 ++++++---- app/main.py | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/customer.py b/app/customer.py index 16020430..d2a5cbde 100644 --- a/app/customer.py +++ b/app/customer.py @@ -31,10 +31,12 @@ def calculate_travel_cost( return round(l_per_km * distance * fuel_price * 2, 2) def calculate_cart_cost(self, shop: Shop) -> int | float: - cart_cost = 0 - for item in self.product_cart: - cart_cost += self.product_cart[item] * shop.products[item] - return cart_cost + if shop.products: + cart_cost = 0 + for item in self.product_cart: + cart_cost += self.product_cart[item] * shop.products[item] + return cart_cost + return 0 def calculate_total_cost( self, diff --git a/app/main.py b/app/main.py index 296ab42c..f1d8df7c 100644 --- a/app/main.py +++ b/app/main.py @@ -13,10 +13,10 @@ def shop_trip() -> None: for customer in customers: print(f"{customer.name} has {customer.money} dollars") - cheapest_shop = [None, 0] + cheapest_shop = [None, float("inf")] for shop in shops: total_cost = customer.calculate_total_cost(fuel_price, shop) - if total_cost < cheapest_shop[1] or cheapest_shop[1] == 0: + if total_cost < cheapest_shop[1]: cheapest_shop[0] = shop cheapest_shop[1] = total_cost print(f"{customer.name}'s trip to "