Build a Python program that simulates the operation of a coffee machine. This project will help you understand the basics of Python programming and how to use PyCharm for development. Please go through the requirement before going for this solution.
# Define initial resources
resources = {
"water": 300,
"milk": 200,
"coffee": 100,
"money": 0.0,
}
# Define drink options and their ingredients
menu = {
"espresso": {"water": 50, "coffee": 18, "cost": 1.5},
"latte": {"water": 200, "milk": 150, "coffee": 24, "cost": 2.5},
"cappuccino": {"water": 250, "milk": 100, "coffee": 24, "cost": 3.0},
}
# Prompt user and check for available resources
def is_resource_sufficient(order_ingredients):
for item in order_ingredients:
if order_ingredients[item] > resources[item]:
print(f"Sorry, there is not enough {item}.")
return False
return True
# Process coins and calculate change
def process_coins():
print("Please insert coins.")
total = int(input("How many quarters?: ")) * 0.25
total += int(input("How many dimes?: ")) * 0.1
total += int(input("How many nickels?: ")) * 0.05
total += int(input("How many pennies?: ")) * 0.01
return total
# Check if user's money is enough for the drink
def is_transaction_successful(money_received, drink_cost):
if money_received >= drink_cost:
change = round(money_received - drink_cost, 2)
print(f"Here is ${change} in change.")
resources["money"] += drink_cost
return True
else:
print("Sorry, that's not enough money. Money refunded.")
return False
# Make coffee and deduct resources
def make_coffee(drink_name, order_ingredients):
for item in order_ingredients:
resources[item] -= order_ingredients[item]
print(f"Here is your {drink_name}. Enjoy!")
# Main coffee machine program
machine_on = True
while machine_on:
user_choice = input("What would you like? (espresso/latte/cappuccino): ").lower()
if user_choice == "off":
machine_on = False
elif user_choice == "report":
for item, quantity in resources.items():
if item != "money":
print(f"{item.capitalize()}: {quantity}")
else:
print(f"Money: ${quantity}")
else:
drink = menu.get(user_choice)
if drink:
if is_resource_sufficient(drink["ingredients"]):
payment = process_coins()
if is_transaction_successful(payment, drink["cost"]):
make_coffee(user_choice, drink["ingredients"])
else:
print("Invalid choice. Please choose a valid drink (espresso/latte/cappuccino) or enter 'off' to turn off the machine.")
One Response