-
-
Save cyvax/66f7891f9c6626413d93cf0dda5e0a77 to your computer and use it in GitHub Desktop.
coffee_machine.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write your code here | |
class CoffeMachine: | |
def __init__(self): | |
# water, milk, beans, cups, money | |
self.inventory = [400, 540, 120, 9, 550] | |
# x water, x milk, x beans, cups, price | |
espresso = (250, 0, 16, 1, 4) | |
latte = (350, 75, 20, 1, 7) | |
cappuccino = (200, 100, 12, 1, 6) | |
self.coffee_list = (espresso, latte, cappuccino) | |
def menu(self, action): | |
if action == "buy": | |
self.buy() | |
elif action == "fill": | |
self.fill() | |
elif action == "take": | |
self.take() | |
elif action == "remaining": | |
self.status() | |
elif action == "exit": | |
exit(0) | |
def status(self): | |
print(f"The coffee machine has:\n" | |
f"{self.inventory[0]} of water\n" | |
f"{self.inventory[1]} of milk\n" | |
f"{self.inventory[2]} of coffee beans\n" | |
f"{self.inventory[3]} of disposable cups\n" | |
f"{self.inventory[4]} of money\n") | |
def fill(self): | |
texts = ["Write how many ml of water do you want to add:", | |
"Write how many ml of milk do you want to add:", | |
"Write how many grams of coffee beans do you want to add:", | |
"Write how many disposable cups of coffee do you want to add:"] | |
for i in range(4): | |
self.inventory[i] += int(input(texts[i])) | |
def take(self): | |
print(f"I gave you ${self.inventory[4]}") | |
self.inventory[4] = 0 | |
def buy(self): | |
coffee = input("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - return menu:") | |
if coffee == "back": | |
return | |
proportion = self.coffee_list[int(coffee) - 1] | |
check = [self.inventory[i] > proportion[i] for i in range(4)] | |
if all(check): | |
print("I have enough resources, making you a coffee!") | |
for i in range(4): | |
self.inventory[i] -= proportion[i] | |
self.inventory[4] += proportion[4] | |
else: | |
missing = [i for i, x in enumerate(check) if not x] | |
ressource = ["water", "milk", "beans", "cups"] | |
for i in missing: | |
print(f"Sorry, not enough {ressource[i]}!") | |
coffe_machine = CoffeMachine() | |
while True: | |
user_action = input("Write action (buy, fill, take, remaining, exit):") | |
coffe_machine.menu(user_action) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment