(Edited 24 seconds later.)
# Define initial resources of the coffee machine
resources = {
'water': 300,
'milk': 200,
'coffee': 100,
'money': 0
}
# Define the cost of each drink
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
}
}
# Define functions for the actions the user can take
def report():
"""Prints a report of the current resource values."""
print(f"Water: {resources['water']}ml")
print(f"Milk: {resources['milk']}ml")
print(f"Coffee: {resources['coffee']}g")
print(f"Money: ${resources['money']}")
def check_resources(drink):
"""Checks if there are enough resources to make the drink."""
for ingredient in drink['ingredients']:
if resources[ingredient] < drink['ingredients'][ingredient]:
print(f"Sorry there is not enough {ingredient}.")
return False
return True
def process_coins():
"""Prompts the user to insert coins and returns the total value."""
print("Please insert coins.")
quarters = int(input("How many quarters?: ")) * 0.25
dimes = int(input("How many dimes?: ")) * 0.10
nickles = int(input("How many nickles?: ")) * 0.05
pennies = int(input("How many pennies?: ")) * 0.01
total = quarters + dimes + nickles + pennies
return total
def check_transaction_successful(drink, total):
"""Checks if the transaction is successful."""
if total < drink['cost']:
print("Sorry that's not enough money. Money refunded.")
return False
else:
change = round(total - drink['cost'], 2)
resources['money'] += drink['cost']
if change > 0:
print(f"Here is ${change} in change.")
return True
def make_coffee(drink):
"""Deducts the ingredients from the resources to make the drink."""
for ingredient in drink['ingredients']:
resources[ingredient] -= drink['ingredients'][ingredient]
print(f"Here is your {user_choice}. Enjoy!")
# Start the program
while True:
user_choice = input("What would you like? (espresso/latte/cappuccino): ")
# Turn off the coffee machine
if user_choice == "off":
break
# Print report of the current resource values
elif user_choice == "report":
report()
# Make a drink
else:
drink = menu[user_choice]
if check_resources(drink):
total = process_coins()
if check_transaction_successful(drink, total):
make_coffee(drink)(Edited 6 minutes later.)
Start a new topic to continue this conversation.
Or browse the latest topics.