Coffee Machine
Mon Sep 05 2022 21:53:20 GMT+0000 (Coordinated Universal Time)
Saved by @yusufalao #python #coffee_machine
import os MENU = { "espresso": { "ingredients": { "water": 50, "coffee": 18, }, "cost": 1.5, }, "latte": { "ingredients": { "water": 200, "milk": 150, "coffee": 24, }, "cost": 2.5, }, "cappuccino": { "ingredients": { "water": 250, "milk": 100, "coffee": 24, }, "cost": 3.0, } } resources = { "water": 300, "milk": 200, "coffee": 100, "money": 0 } current_resources = resources # TODO: 1 Print report def report(item): print("*"*20) print(f"* Water : {item['water']}ml") if "milk" in item: print(f"* Milk : {item['milk']}ml") print(f"* Coffee : {item['coffee']}g") if "money" in item: print(f"* Money : ${item['money']:.2f}") print("*"*20) # TODO: 3 Resources check def sufficient_resources(item): water = False milk = False coffee = False if resources["water"] >= item["ingredients"]["water"]: water = True else: print("Sorry, there's not enough water") if resources["coffee"] >= item["ingredients"]["coffee"]: coffee = True else: print("Sorry, there's not enough coffee") if "milk" in item["ingredients"]: if resources["milk"] >= item["ingredients"]["milk"]: milk = True else: print("Sorry, there's not enough milk") else: milk = True return water and milk and coffee # TODO: 4 Process payment def process_coins(item): cost = item["cost"] amount = cost result = False print(f"Please insert coins to the value of ${cost:.2f}.") quarters = int(input(" How many quarters?: ")) amount -= (quarters * 0.25) if amount > 0: dimes = int(input(" How many dimes?: ")) amount -= (dimes * 0.1) if amount > 0: nickels = int(input(" How many nickels?: ")) amount -= (nickels * 0.05) if amount > 0: pennies = int(input(" How many pennies?: ")) amount -= (pennies * 0.1) if amount > 0: print(f"Sorry, that's not enough money. Your ${amount:.2f} has been refunded.") return False elif amount < 0: print(f"Here is ${abs(amount):.2f} in change.") return True else: return True def update_resources(item): water = resources["water"] - item["ingredients"]["water"] coffee = resources["coffee"] - item["ingredients"]["coffee"] if "milk" in item["ingredients"]: milk = resources["milk"] - item["ingredients"]["milk"] else: milk = resources["milk"] money = resources["money"] + item["cost"] updated_resources = { "water": water, "milk": milk, "coffee": coffee, "money": money, } return updated_resources clear = lambda: os.system("cls") # Main program flow =========================================================== machine_on = True choices = ["report", "off", "espresso", "latte", "cappuccino"] drink = "" # TODO: 2 User prompt while machine_on: # clear() prompt = "" while prompt not in choices: prompt = input(" What would you like? (espresso/latte/cappuccino): ").lower() if prompt == "off": machine_on = False elif prompt == "report": report(resources) elif prompt in choices: drink = MENU[prompt] if type(drink) != str and prompt != "report": if not sufficient_resources(drink): pass else: if process_coins(drink): resources = update_resources(drink) # print(resources) print(f"Here is your {prompt}. Enjoy!☕")
Comments