-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (118 loc) · 4.2 KB
/
Copy pathmain.py
File metadata and controls
144 lines (118 loc) · 4.2 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
COFFEE_RECIPES = {
"1": {
"name": "espresso",
"water": 250,
"milk": 0,
"coffee": 16,
"cups": 1,
"cost": 4
},
"2": {
"name": "latte",
"water": 350,
"milk": 75,
"coffee": 20,
"cups": 1,
"cost": 7
},
"3": {
"name": "cappuccino",
"water": 200,
"milk": 100,
"coffee": 12,
"cups": 1,
"cost": 6
}
}
def print_machine_status(machine_content):
"""Function to display the current status of the coffee machine."""
print("The coffee machine has:")
print(f"{machine_content['water']} ml of water")
print(f"{machine_content['milk']} ml of milk")
print(f"{machine_content['coffee']} g of coffee beans")
print(f"{machine_content['cups']} disposable cups")
print(f"${machine_content['money']} of money")
print()
def check_resources(machine_content, recipe):
"""
Function to check if there are enough resources to make a coffee. Function takes
dict machine_content and dict recipe as arguments. Returns tuple (enough_content, missing_resources).
"""
enough_content = True
missing_resources = None
for resource, amount in recipe.items():
if resource == "cost" or resource == "name":
continue
if machine_content.get(resource, 0) < amount:
enough_content = False
missing_resources = resource
return enough_content, missing_resources
return enough_content, missing_resources
def buy_coffee(machine_content):
"""
Function to buy coffee from the machine. Takes machine content as argument.
According to the coffee type, reduces machine content from provided machine content.
Returns nothing.
"""
print("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:")
selection = input()
if selection == "back":
return
recipe = COFFEE_RECIPES[selection]
enough_content, missing_resources = check_resources(machine_content, recipe)
if not enough_content:
print(f"Sorry, not enough {missing_resources}.")
return
print(f"I have enough resources, making you a coffee!")
machine_content["water"] -= recipe["water"]
machine_content["milk"] -= recipe["milk"]
machine_content["coffee"] -= recipe["coffee"]
machine_content["money"] += recipe["cost"]
machine_content["cups"] -= recipe["cups"]
print()
def fill_machine(machine_content):
# TODO Implement filling the machine with resources, UI and business logic are still mixed
"""
Function to fill the coffee machine with resources. Takes machine content as argument.
Adds specified amounts of water, milk, coffee beans, and disposable cups to the machine content.
Returns nothing.
"""
print("Write how many ml of water you want to add:")
machine_content["water"] += int(input())
print("Write how many ml of milk you want to add:")
machine_content["milk"] += int(input())
print("Write how many grams of coffee beans you want to add:")
machine_content["coffee"] += int(input())
print("Write how many disposable cups you want to add:")
machine_content["cups"] += int(input())
print()
def withdraw_money(machine_content):
"""Function to withdraw money from the machine. Takes machine content as argument. Returns nothing."""
print(f"I gave you {machine_content['money']}")
machine_content["money"] = 0
print()
def main():
machine_content = {
"water": 400,
"milk": 540,
"coffee": 120,
"cups": 9,
"money": 550
}
while True:
action = input("Write action (buy, fill, take, remaining, exit):")
if action == "buy":
buy_coffee(machine_content)
elif action == "fill":
fill_machine(machine_content)
elif action == "take":
withdraw_money(machine_content)
elif action == "remaining":
print_machine_status(machine_content)
elif action == "exit":
break
else:
pass
# print("Invalid action. Please choose 'buy', 'fill', 'take', 'remaining', or 'exit'.")
if __name__ == "__main__":
main()