-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (111 loc) · 3.96 KB
/
main.py
File metadata and controls
133 lines (111 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# A dictionary that stores the type of drink, its ingredients and the quantity it takes #
MENU = {
"espresso": {
"ingredients": {
"water": 50,
"milk": 0,
"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,
}
}
# Contains the total amount of resources available.
resources = {
"water": 300,
"milk": 200,
"coffee": 100,
"money": 0,
}
# A global variable used to store the inputted drink.
drink = "latte"
# Checks to see if there is enough ingredients to complete the order.
def check():
index = 0
for i in (MENU[drink]["ingredients"]):
beverage = MENU[drink]["ingredients"][i]
compare = resources[i]
index += 1
if beverage <= compare and index == 3:
return True
# Will start the loop again if ingredients are insufficient
elif beverage > compare:
print(f"Sorry there is not enough {i}.")
machine()
def coins(drink_cost):
# After the drink name is inputted the user is asked to provide various coins
# as payment.
print("Please insert coins.")
quarters = float(input("how many quarters?: "))
dimes = float(input("how many dimes?: "))
nickles = float(input("how many nickles?: "))
pennies = float(input("how many pennies?: "))
# Check transaction was successful
cost = drink_cost
payment = round((0.25 * quarters) + (0.1 * dimes) + (0.05 * nickles) + (0.01 * pennies), 2)
print(f"${payment:.2f}")
# If enough money has been inputted it is added to the resource variable and returning true
# If too much money has been inputted it will return the excess.
if payment > cost:
refund = payment - cost
print(f"Here is ${refund:.2f} in change.")
resources["money"] += cost
return True
elif payment == cost:
print("Thanks for the exact payment")
resources["money"] += cost
return True
# If not enough money has been inputted a message is outputted to indicate as such.
# the program loop is started again.
elif payment < cost:
print("Sorry that's not enough money. Money refunded.")
machine()
# On successful checking that enough ingredients are available the amount is
# deducted from the resources variable and the success message is outputted
def make(beverage, ingredients):
for i in ingredients:
resources[i] -= ingredients[i]
print(f"Here is your {beverage} ☕️. Enjoy!")
# Prompt the user by asking “What would you like?” to start the program.
def machine():
global drink
drink = input("What would you like? (espresso/latte/cappuccino): ").lower()
# Turn off the Coffee Machine by entering prompt off
if drink == "off":
print("Goodbye")
exit()
# Print a report of all the machines resources.
elif drink == "report":
print(f"Water: {resources['water']}ml\n"
f"Milk: {resources['milk']}ml\n"
f"Coffee: {resources['coffee']}g\n"
f"Money: ${resources['money']:.2f}\n")
machine()
# Check resources are sufficient to make drink order.
elif drink == "espresso" or "latte" or "cappuccino":
if check() is True:
# Process Coins
if coins(MENU[drink]["cost"]) is True:
# Make drink
make(drink, MENU[drink]["ingredients"])
machine()
else:
print("Sorry, can you repeat that")
# The loop will continue until the program is stopped or off is inputted.
machine()
machine()