I tried using functions for the coffee machine program, but it's just not working out well. Especially the fact that you cannot break out of it.
My present code for today.
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,
}
coffee_price = 0
quarters = 0
dimes = 0
nickels = 0
pennies = 0
water_needed = 0
milk_needed = 0
coffee_needed = 0
def report():
"""outputs a present state report of the coffee machine"""
print(f"Water: {resources["water"]}")
print(f"Milk: {resources["milk"]}")
print(f"Coffee: {resources["coffee"]}")
print(f"Money: {resources["money"]}")
def resource_sufficient():
"""checks the amount of resources remaining with the amount of resources needed to make the selected coffee"""
if water_needed > resources["water"]:
print("Sorry, there is not enough water.")
elif milk_needed > resources["milk"]:
print("Sorry, there is not enough milk.")
elif coffee_needed > resources["coffee"]:
print("Sorry, there is not enough coffee.")
def coin_collector_and_calculator():
"""collects coins, calculates total amount inserted and returns change or refunds where necessary"""
print("Please insert coins.")
quarters = float(input("how many quarters?: "))
dimes = float(input("how many dimes?: "))
nickels = float(input("how many nickels?: "))
pennies = float(input("how many pennies?: "))
total_quarters = 0.25 * quarters
total_dimes = 0.10 * dimes
total_nickels = 0.05 * nickels
total_pennies = 0.01 * pennies
total_amount_inserted = total_quarters + total_dimes + total_nickels + total_pennies
if total_amount_inserted < coffee_price:
print("Sorry that's not enough money. Money refunded.")
else:
make_coffee()
change = total_amount_inserted - coffee_price
print(f"Here is ${change:.2f} dollars in change.")
print(f"Here is your {choice}. Enjoy!")
def make_coffee():
"""creates coffee and calculates the amount of ingredients used and total money collected"""
global water_needed, milk_needed, coffee_needed, coffee_price
if choice == "espresso":
water_needed = MENU["espresso"]["ingredients"]["water"]
milk_needed = 0
coffee_needed = MENU["espresso"]["ingredients"]["coffee"]
coffee_price = MENU["espresso"]["cost"]
elif choice == "latte":
water_needed = MENU["latte"]["ingredients"]["water"]
milk_needed = MENU["latte"]["ingredients"]["milk"]
coffee_needed = MENU["latte"]["ingredients"]["coffee"]
coffee_price = MENU["latte"]["cost"]
elif choice == "cappuccino":
water_needed = MENU["cappuccino"]["ingredients"]["water"]
milk_needed = MENU["cappuccino"]["ingredients"]["milk"]
coffee_needed = MENU["cappuccino"]["ingredients"]["coffee"]
coffee_price = MENU["cappuccino"]["cost"]
update_report()
def update_report():
"""updates the coffee machine's report content by taking away used items and adding the money paid for the coffee"""
resources["water"] = resources["water"] - water_needed
resources["milk"] = resources["milk"] - milk_needed
resources["coffee"] = resources["coffee"] - coffee_needed
resources["money"] = resources["money"] + coffee_price
while True:
choice = input("What would you like? (espresso/latte/cappuccino): ")
if choice == "off":
break
if choice == "report":
report()
break
resource_sufficient()
coin_collector_and_calculator()
To be modified tomorrow. :)
Comments
Post a Comment