horozov86

Unit_tests_pet_shop_31.03.2022

Aug 2nd, 2023
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. class PetShop:
  2.     def __init__(self, name: str):
  3.         self.name = name
  4.         self.food = {}
  5.         self.pets = []
  6.  
  7.     def add_food(self, name: str, quantity: float):
  8.         if quantity <= 0:
  9.             raise ValueError('Quantity cannot be equal to or less than 0')
  10.  
  11.         if name not in self.food:
  12.             self.food[name] = 0
  13.         self.food[name] += quantity
  14.         return f"Successfully added {quantity:.2f} grams of {name}."
  15.  
  16.     def add_pet(self, name: str):
  17.         if name not in self.pets:
  18.             self.pets.append(name)
  19.             return f"Successfully added {name}."
  20.         raise Exception("Cannot add a pet with the same name")
  21.  
  22.     def feed_pet(self, food_name: str, pet_name: str):
  23.         if pet_name not in self.pets:
  24.             raise Exception(f"Please insert a valid pet name")
  25.  
  26.         if food_name not in self.food:
  27.             return f'You do not have {food_name}'
  28.  
  29.         if self.food[food_name] < 100:
  30.             self.add_food(food_name, 1000.00)
  31.             return "Adding food..."
  32.  
  33.         self.food[food_name] -= 100
  34.         return f"{pet_name} was successfully fed"
  35.  
  36.     def __repr__(self):
  37.         return f'Shop {self.name}:\n' \
  38.                f'Pets: {", ".join(self.pets)}'
  39.  
Add Comment
Please, Sign In to add comment