-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay15CoffeeMachine.py
More file actions
185 lines (153 loc) · 4.67 KB
/
Day15CoffeeMachine.py
File metadata and controls
185 lines (153 loc) · 4.67 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# coin operated
# penny = .01 $
# nickel = .05 $
# dime = .10 $
# quarter = .25 $
# requirements:
# print a report
# how much resources left
# example:
# > report
# Water: 300ml
# Milk: 200ml
# Coffee: 100g
# Money: 0 $
# check if the resources are sufficient
import time
machine_state = {
"water": {
"value": 300,
"unit": "ml"
},
"milk": {
"value": 300,
"unit": "ml"
},
"coffee": {
"value": 100,
"unit": "g"
},
"money": {
"value": 0,
"unit": "$"
},
}
machine_items = {
"espresso": {
"index": "1",
"label": "Espresso",
"water": 50,
"milk": 0,
"coffee": 15,
"money": 1.5,
},
"latte": {
"index": "2",
"label": "Latte",
"water": 200,
"milk": 150,
"coffee": 24,
"money": 2.5,
},
"cappuccino": {
"index": "3",
"label": "Cappuccino",
"water": 250,
"milk": 100,
"coffee": 24,
"money": 2.5,
}
}
def print_machine_report():
for item_key in machine_state:
item_value = machine_state[item_key]["value"]
item_unit = machine_state[item_key]["unit"]
print(f"{item_key}: {item_value} {item_unit}")
print("===============")
def program_prompt():
print("Welcome to Coffee Machine Inc.")
for item_key in machine_items:
print(f"{machine_items[item_key]['index']}. {machine_items[item_key]['label']}")
return input("What would you like? (enter item number) / or type report \n> ")
def accept_money_and_return_total():
pennies = float(input("How many pennies\n> "))
dimes = float(input("How many dimes\n> "))
quarters = float(input("How many quarters\n> "))
nickels = float(input("How many nickels\n> "))
return (pennies * .01) + (nickels * .05) + (quarters * .25) + (dimes * .1)
def print_invalid_choice():
print("Invalid Choice.")
print("===============")
def print_not_enough_money():
print("Not enough money. Money refunded.")
print("===============")
def machine_has_enough_resources(choice):
if choice['coffee'] > 0 and machine_state["coffee"]["value"] - choice['coffee'] < 0:
print("not enough coffee")
print("===============")
return False
elif choice['water'] > 0 and machine_state["water"]["value"] - choice['water'] < 0:
print("not enough water")
print("===============")
return False
elif choice['milk'] > 0 and machine_state["milk"]["value"] - choice['milk'] < 0:
print("not enough milk")
print("===============")
return False
else:
return True
def create_machine_item(choice):
machine_state["coffee"] = machine_state["coffee"]["value"] - choice['coffee']
machine_state["water"] = machine_state["water"]["value"] - choice['water']
machine_state["milk"] = machine_state["milk"]["value"] - choice['milk']
print('Preparing your drink.', end='')
time.sleep(1)
print('.', end='')
time.sleep(1)
print('.', end='')
time.sleep(1)
print('.')
time.sleep(1)
print('Pouring.', end='')
time.sleep(1)
print('.', end='')
time.sleep(1)
print('.', end='')
time.sleep(1)
print('.', end='')
def item_ready(choice):
print(f"Your {choice['label']} is ready! Enjoy!")
print("===============")
def print_not_enough_resources():
print("Sorry the machine does not have enough resources at the moment.")
print("===============")
def get_selected_item(user_choice_string):
for item_key in machine_items:
if machine_items[item_key]['index'] == user_choice_string:
return machine_items[item_key]
def print_goodbye():
print('Thank you for choosing the Coffee Machine Inc, Goodbye!')
while True:
user_choice_string = program_prompt()
if user_choice_string == 'report':
print_machine_report()
continue
if user_choice_string == 'off':
print_goodbye()
break
selected_machine_item = get_selected_item(user_choice_string)
if selected_machine_item == None:
print_invalid_choice()
continue
if not machine_has_enough_resources(selected_machine_item):
print_not_enough_resources()
continue
total_money_in_dollars = accept_money_and_return_total()
if total_money_in_dollars < selected_machine_item['money']:
print_not_enough_money()
continue
change = total_money_in_dollars - selected_machine_item['money']
create_machine_item(selected_machine_item)
item_ready(selected_machine_item)
print(f"Your change is: {change} $")
print("===============")