-
Notifications
You must be signed in to change notification settings - Fork 0
/
vending_machine.py
95 lines (70 loc) · 1.89 KB
/
vending_machine.py
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
items_in_stock = [
{
"item_id": 0,
"item_name": "Milky Bar",
'item_price': 60,
},
{
"item_id": 1,
"item_name": "Fanta",
'item_price': 90,
},
{
"item_id": 2,
"item_name": "Kurkure",
'item_price': 25,
},
{
"item_id": 3,
"item_name": "Thumbs Up",
'item_price': 90,
},
{
"item_id": 4,
"item_name": "Wai-Wai",
'item_price': 20,
},
]
the_item = []
reciept = """
\t\tPRODUCT -- PRICE
"""
sum = 0
run = True
print("------- Vending Machine with Python-------\n\n")
print("----------The Items In Stock Are----------\n\n")
for i in items_in_stock:
print(f"Item: {i['item_name']} --- Price: {i['item_price']} --- Item ID: {i['item_id']}")
def machine(items_in_stock, run, the_item):
while run:
buy_item = int(input("\n\nEnter the item code of the product you want to buy: "))
if buy_item < len(items_in_stock):
the_item.append(items_in_stock[buy_item])
else:
print("THE PRODUCT ID IS WRONG!")
more_items = str(input("press any key to add more items and press q to quit.. "))
if more_items == "q":
run = False
rec_bool = int(input(("1. print the reciept? 2. only print the total sum .. ")))
if rec_bool == 1:
print(create_reciept(the_item, reciept))
elif rec_bool == 2:
print(sum(the_item))
else:
print("INVALID ENTRY")
def sum(the_item):
sum = 0
for i in the_item:
sum += i["item_price"]
return sum
def create_reciept(the_item, reciept):
for i in the_item:
reciept += f"""
\t{i["item_name"]} -- {i['item_price']}
"""
reciept += f"""
\tTotal --- {sum(the_item)}
"""
return reciept
if __name__ == "__main__":
machine(items_in_stock, run, the_item)