Skip to content

Commit dbb650e

Browse files
Create coffeMachineCompact.py
1 parent a57c304 commit dbb650e

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

coffeMachineCompact.py

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

0 commit comments

Comments
 (0)