advanced coffee machine in python

Solutions on MaxInterview for advanced coffee machine in python by the best coders in the world

showing results for - "advanced coffee machine in python"
Sophia
28 Mar 2019
1import os
2MENU = {
3    "espresso": {
4        "ingredients": {
5            "water": 50,
6            "coffee": 18,
7        },
8        "cost": 2,
9    },
10    "latte": {
11        "ingredients": {
12            "water": 200,
13            "milk": 150,
14            "coffee": 24,
15        },
16        "cost": 5,
17    },
18    "cappuccino": {
19        "ingredients": {
20            "water": 250,
21            "milk": 100,
22            "coffee": 24,
23        },
24        "cost": 15,
25    }
26}
27
28profit = 0
29resources = {
30    "water": 1000,
31    "milk": 700,
32    "coffee": 500,
33}
34
35
36def is_resource_sufficient(order_ingredients):
37    """Returns True when order can be made, False if ingredients are insufficient."""
38    for item in order_ingredients:
39        if order_ingredients[item] > resources[item]:
40            print(f"​Sorry there is not enough {item}.")
41            return False
42    return True
43
44
45def process_coins():
46    """Returns the total calculated from coins inserted."""
47    print("Please insert coins.")
48    total = int(input("how many quarters?: ")) * 0.25
49    total += int(input("how many dimes?: ")) * 0.1
50    total += int(input("how many nickles?: ")) * 0.05
51    total += int(input("how many pennies?: ")) * 0.01
52    return total
53
54def Buy_the_resorses():
55    global resources
56    resources = {
57    "water": 1000,
58    "milk": 1000,
59    "coffee": 700,
60    }
61
62def is_transaction_successful(money_received, drink_cost):
63    """Return True when the payment is accepted, or False if money is insufficient."""
64    if money_received >= drink_cost:
65        change = round(money_received - drink_cost, 2)
66        print(f"Here is ${change} in change.")
67        global profit
68        profit += drink_cost
69        return True
70    else:
71        print("Sorry that's not enough money. Money refunded.")
72        return False
73
74
75def make_coffee(drink_name, order_ingredients):
76    """Deduct the required ingredients from the resources."""
77    for item in order_ingredients:
78        resources[item] -= order_ingredients[item]
79    print(f"Here is your {drink_name} ☕️. Enjoy!")
80
81
82is_on = True
83
84while is_on:
85    try:
86        choice = str(input("​What would you like? (espresso / latte / cappuccino): "))
87        if choice == "off":
88            is_on = False
89        elif choice == "report":
90            print(f"Water: {resources['water']}ml")
91            print(f"Milk: {resources['milk']}ml")
92            print(f"Coffee: {resources['coffee']}g")
93            print(f"Money: ${profit}")
94        elif choice == "Buy the resorses":
95            Buy_the_resorses()
96            profit -= 10
97        elif choice == "clear":
98            os.system("cls")
99        else:
100            drink = MENU[choice]
101            if is_resource_sufficient(drink["ingredients"]):
102                payment = process_coins()
103                if is_transaction_successful(payment, drink["cost"]):
104                    make_coffee(choice, drink["ingredients"])
105    except :
106        print("An unexpected error occurred")
107        print("try again")