In: Computer Science
def vend():
a = {'key': '0', 'item': 'choc', 'price': 1.50, 'stock': (2)}
b = {'key': '1', 'item': 'pop', 'price': 1.75, 'stock': 1}
c = {'key': '2', 'item': 'chips', 'price': 2.00, 'stock': 3}
d = {'key': '3', 'item': 'gum', 'price': 0.50, 'stock': 1}
e = {'key': '4', 'item': 'mints', 'price': 0.75, 'stock': 3}
items = [a, b, c, d, e]
def show(items):
cim = 0
for item in items:
if item.get('stock') == 0:
items.remove(item)
for item in items:
print(item.get('key'), item.get('item'),item.get('price'), item.get('stock'))
selected = input('select item: ')
for key in items:
if selected == key.get('key'):
selected = key
price = selected.get('price')
while cim < price:
cim = cim + float(input('insert ' + str(price - cim) + ': '))
print('vend: ' + selected.get('item'))
selected['stock'] -= 1
cim -= price
print('refunded: ' + str(cim))
if cim != 0:
print("credit: $0.00")
break
else:
continue
show(items)
vend()
instead of showing the items can you make it so the user has to input "items"
and instead can you make it so it shows credit everytime you insert a coin
and instead of insert numbers can you make it you have to type dime to insert .10 and nickel to insert .05 and quarter to insert .25
and can you make it possible for the user to restock something
I hope you are the same person who asked this question before, ( I recognize the changes made by me) .If you need to code these , then you need to refactor the whole code ,because, it is hard to complete the code with this requirements.
Foe example, if you want to read the item, the code should bu as follows.
def vend():
items = []
item = ["choc",1.50,2] # in the order [item name , price , stock]
items.append(item)
for item in items:
if item[-1] == 0:
items.remove(item)
cim = 0
ITEM = input("Enter the item name : ")
flag = False
for item in items:
if ITEM == item[0]: # if the item already exists,then reduce the stock
items[-1] -= 1 # reduce the stock
flag = True
temp = [item[0],item[1],item[2]]
break
if not flag:
temp = [ITEM , input("Enter the price :"), 1]
items.extend(temp)
vend()
Some more information is required.
Just look at the comment section.....