IFT-300 Adapt Find in Files lesson samples to Python 2.7

IJ-CR-16198

GitOrigin-RevId: 54a7ee531d436430d0a27927e610471d36f7adfc
This commit is contained in:
Konstantin Hudyakov
2021-10-29 17:07:53 +03:00
committed by intellij-monorepo-bot
parent 1c1d590871
commit bbb4d7ec3c
3 changed files with 9 additions and 9 deletions

View File

@@ -1,31 +1,31 @@
from util.util import FRUITS
from util import FRUITS
class Warehouse:
# Fruit name to amount of it in warehouse
entry = {} # Apple, banana, etc...
def __init__(self) -> None:
def __init__(self):
for fruit in FRUITS:
self.entry[fruit] = 0
# fruit name from util.FRUITS (mango, apple...)
def add_fruits(self, fruit_name, quantity) -> None:
def add_fruits(self, fruit_name, quantity):
cur_quantity = self.entry.get(fruit_name)
if cur_quantity is not None:
self.entry[fruit_name] = cur_quantity + quantity
else:
raise KeyError(f"Not found fruit with name: {fruit_name}")
raise KeyError("Not found fruit with name: " + fruit_name)
def take_fruit(self, fruit_name) -> bool:
def take_fruit(self, fruit_name):
cur_quantity = self.entry.get(fruit_name)
if cur_quantity is None:
raise KeyError(f"Not found fruit with name: {fruit_name}")
raise KeyError("Not found fruit with name: " + fruit_name)
elif cur_quantity > 0:
self.entry[fruit_name] = cur_quantity - 1
return True
return False
def print_all_fruits(self) -> None:
def print_all_fruits(self):
for fruit, quantity in self.entry.items():
print(f"{fruit}: {quantity}")
print(fruit + ": " + str(quantity))

View File

@@ -1,4 +1,4 @@
from warehouse.warehouse import Warehouse
from warehouse import Warehouse
warehouse = Warehouse()
warehouse.add_fruits('peach', 3)