Day 8-9: Coffee Machine Program with Dictionaries

May 26-27th

·

2 min read

Finally finished my first coffee machine program. Made lots of new functions today. It's kind of hard to remember where to put the arguments in loops while making them but I'll get the hang of it. Next is another coffee machine but done with OOP.

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,
}
#TODO print report of copy machine resources


bank = 0


#TODO check if there are enough resources to make the drinks
def enough_resources(drink_ingredients):
   for  items in drink_ingredients:
        if drink_ingredients[items] >= resources[items]:
            print(f"Sorry, there is not enough{item}")
            return False
   return True

def coin_machine():
    """Returns the total calculated from coins inserted."""
    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 nickles?: ")) * 0.05
    total += int(input("how many pennies?: ")) * 0.01
    return total



# TODO make sure there's enough money for the selected drink

def enough_money(money_recieved, cost_of_drink):
    """ return TRUE if funds are sufficient, FALSE if not """
    if money_recieved >= cost_of_drink:
        change = round(money_recieved - cost_of_drink, 2)
        print(f"Here is ${change} in change.")
        global bank
        bank += cost_of_drink
        return True
    if money_recieved < cost_of_drink:
        print("Sorry, that's not enough money.")
        return False

def coffee_maker(drink_name, order_ingredients):
    """ Take number of ingredients from resources """
    for items in order_ingredients:
        resources[items] -= order_ingredients[items]
    print(f"Here is your {drink_name}. Enjoy! ☕️")


machine_on = True

while machine_on:
    order = input("What would you like? ").lower()
    if order == "report":
        print(f"There is Milk: {resources['milk']}ml.")
        print(f"There is Water: {resources['water']}ml .")
        print(f"There is Coffee: {resources['coffee']}g.")
        print(f"Money: ${bank}")
    elif order == "off":
        machine_on == False
    else:
        drink = MENU[order]
        if enough_resources(drink["ingredients"]):
            payment = coin_machine()
        if enough_money(payment, drink["cost"]):
            coffee_maker(order, drink["ingredients"])