Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
4.4 kB
5
Indexable
Never
from idlelib.mainmenu import menudefs

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,
}

def check_resources(coffee_machine_resources, coffee_choice):
    """Verifies if the coffee machine has enough ingredients to make the coffee."""
    for ingredient in MENU[coffee_choice]["ingredients"]:
        if coffee_machine_resources[ingredient] < MENU[coffee_choice]["ingredients"][ingredient]:
            print(f"Sorry there is not enough {ingredient}.")
            return "reset"

def check_refund(coffee_choice, money_in_machine):

    if money_in_machine < MENU[coffee_choice]["cost"]:
        print(f"Sorry that's not enough money. Money refunded")
        return "refund"

def insert_coins():
    """Returns the amount of change the user inserted into the coffee machine."""
    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?: "))
    return (quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)

def print_report():
    """Prints the resources available for the coffee machine"""
    water = resources["water"]
    milk = resources["milk"]
    coffee = resources["coffee"]
    print(f"Water: {water}ml\nMilk: {milk}ml\nCoffee: {coffee}g\nMoney: ${format(money, ".2f")}")

def transaction(coffee_choice, money_in_machine):
    change =  money_in_machine - MENU[coffee_choice]["cost"]
    print(f"Here is ${format(change, ".2f")} in change.")
    print(f"Here is your {coffee_choice} ☕ Enjoy!")
    return change

def request_ingredients():
    """Request the owner of the coffee machine to refill it."""
    request = input("Would you like to request the coffee machine's owner to refill it? Type 'y' or 'n': ")
    if request == "y":
        return True
    else:
        return False


# Define money variable
money = 0

def coffe_machine():
    
    buy_coffee = True
    while buy_coffee:
        # Ask the user what coffe they want
        user_choice = input("What would you like? (espresso/latte/cappuccino): ").lower()
    
        if user_choice == "report":
            print_report()
            continue
        elif user_choice == "espresso" or user_choice == "latte" or user_choice == "cappuccino":
            # Verify whether there are enough resources left
            resource_status = check_resources(coffee_machine_resources=resources, coffee_choice=user_choice)
            if resource_status == "reset":
                user_request = request_ingredients()
                if user_request:
                    print("The owner has refilled the coffee machine!")
                    resources["water"] += 300
                    resources["milk"] += 200
                    resources["coffee"] += 100
                    continue
                elif not user_request:
                    print("Have a good day!")
                    quit()
    
            # Get the user to insert money into the machine and verify if he has enough
            money += insert_coins()
            refund_status = check_refund(coffee_choice=user_choice, money_in_machine=money)
            if refund_status == "refund":
                money = 0
                continue
    
            # Save the amount of money left after the transaction
            money_remaining = transaction(user_choice, money)
        else:
            print("Good bye!")
            quit()
    
        # update the resources and the money left after the transaction has been completed
        for ingredient in MENU[user_choice]["ingredients"]:
            resources[ingredient] -= MENU[user_choice]["ingredients"][ingredient]
        money = money_remaining

coffe_machine()
Leave a Comment