Repository navigation
Expand file tree
/
Copy pathVending_Machine.py
More file actions
118 lines (86 loc) · 2.87 KB
/
Copy pathVending_Machine.py
File metadata and controls
118 lines (86 loc) · 2.87 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
#==================================================================
# Global Variables
#==================================================================
vending_machine = {
"Chips" : {
"quantity" : 20,
"price" : 2.00
},
"Soda" : {
"quantity" : 30,
"price" : 1.50
},
"Gum" : {
"quantity" : 40,
"price" : 0.75
},
"Candy" : {
"quantity" : 40,
"price" : 0.50
},
"Water" : {
"quantity" : 30,
"price" : 1.00
},
}
#==================================================================
# Functions
#==================================================================
def insert_money():
enter_in_money = f"\nEnter the money you would like to spend: "
price = int(float(input(enter_in_money)))
while price <= 0:
print(f"\nERROR: {price} is less than or equal to 0!\n")
price = int(float(input(enter_in_money)))
return price
def get_menu(vending_machine):
items = list(vending_machine.keys())
items.insert(0, "")
menu = '''
The Menu
================
'''
for item in items:
index = items.index(item)
if item == "":
continue
menu += f"{index}. {item}\n"
menu += f"\nEnter you choice [1 - {len(items)-1}]: "
return menu
def get_user_choice(vending_machine):
items = list(vending_machine.keys())
items.insert(0, "")
last_index = len(items)-1
# get the choice of items
menu = get_menu(vending_machine)
user_choice = int(float(input(menu)))
while not 1 <= user_choice <= last_index:
print(f"\nERROR: {user_choice} is an invalid choice!\n")
user_choice = int(float(input(menu)))
# get quantity
item = items[user_choice]
available_quantity = vending_machine.get(item).get("quantity")
quantity = int(float(input(f"Enter the quantity [1 - {available_quantity}]: ")))
while not 1 <= quantity <= available_quantity:
print(f"\nERROR: {quantity} is an invalid quantity!\n")
quantity = int(float(input(f"Enter the quantity [1 - {available_quantity}]: ")))
return (item, quantity)
def purchase_items():
shopping_list = dict()
more_items = "y"
while more_items.lower() == "y":
(item, quantity) = get_user_choice(vending_machine)
print(item, quantity)
# Update Shopping List
if item in shopping_list.keys():
shopping_list[item] = shopping_list[item] + quantity
else:
shopping_list[item] = quantity
more_items = input("Would you like to buy more items? (y/n): ")
return shopping_list
#==================================================================
# Main Program starts here
#==================================================================
insert_money()
shopping_list = purchase_items()
print(shopping_list)