Reputation: 1
# Vending Machine Simulation
print ("Welcome to the vending machine simulation")
print ("Please enter three coins. Enter as 10 (10p), 20 (20p), 50 (50p) or 100 (£1)")
c1 = int(input("Please enter your first coin: "))
while c1 not in (10, 20, 50, 100):
print("That is not an accepted coin value")
c1 = int(input("Please re-enter your first coin: "))
c2 = int(input("Please enter your second coin: "))
while c2 not in (10, 20, 50, 100):
print("That is not an accepted coin value")
c2 = int(input("Please re-enter your second coin: "))
c3 = int(input("Please enter your third coin: "))
while c3 not in (10, 20, 50, 100):
print("That is not an accepted coin value")
c3 = int(input("Please re-enter your third coin: "))
total = int(c1 + c2 + c3)
print ("You have",total," in credit.")
print ("Thank you for entering your coins. Prices: Hydration: £1, Nutrition: 60p")
Hydration = ("Cola","Fanta","Coffee")
Nutrition = ("Bacon", "Steak","Beans")
print ("Hydration Menu:")
print (Hydration)
print ("Nutrition Menu:")
print (Nutrition)
cost =[]
cost[Hydration] = 100
cost[Nutrition] = 60
pro1 = input ("What would you like, sir/madam?: ")
while pro1 not in (Hydration, Nutrition):
print (total)
pro1 = input ("You entered an invalid term. What would you like, sir/madam?: ")
while cost[pro1] > total:
print ("You do not have enough credit to purchase this product.")
pro1 = input ("What would you like, sir/madam?: ")
if cost[pro1] < total:
total = total - cost[pro1]
print ("Thank you.",total)
pro2 = input ("Would you like anything else, sir/madam?: ")
while pro2 not in (Hydration, Nutrition):
print (total)
pro2 = input ("You entered an invalid term. What would you like, sir/madam?: ")
while cost[pro2] > total:
print ("You do not have enough credit to purchase this product.")
pro2 = input ("Would you like anything else, sir/madam?: ")
if cost[pro2] < total:
total = total - cost[pro2]
print ("Thank you.",total)
pro3 = input ("You have quite the appetite. Would you like anything else, sir/madam?: ")
while pro3 not in (Hydration, Nutrition):
print (total)
pro3 = input ("You entered an invalid term. What would you like, sir/madam?: ")
while cost[pro3] > total:
print ("You do not have enough credit to purchase this product.")
pro3 = input ("Would you like anything else, sir/madam?: ")
if cost[pro3] < total:
total = total - cost[pro3]
print ("Thank you.",total)
I've uploaded my entire code, because I guessed there'd be bits I was leaving out that you needed to know.
I was going to try to put the bits I'm having a problem with in bold, but that didn't work. So,
Hydration = ("Cola","Fanta","Coffee")
Nutrition = ("Bacon", "Steak","Beans")
print ("Hydration Menu:")
print (Hydration)
print ("Nutrition Menu:")
print (Nutrition)
cost =[]
cost[Hydration] = 100
cost[Nutrition] = 60
I'm trying to assign the variables Nutrition and Hydration to the values of 60 and 100 so the program will know how much to take away from your credit, but it keeps returning that the variable 'cost' is not defined. Also, the code doesn't seem to recognise that the products (as seen above) are valid terms despite the fact that they should be connected to the variables Hydration and Nutrition.
As you can see, I'm not very knowledgeable in the art of code. That's why I'm in need of help. Thank you in advance.
Upvotes: 0
Views: 58
Reputation: 113988
you need a dictionary not a list
cost ={}
cost["Hydration"] = 100
cost["Nutrition"] = 60
print cost["Nutrition"]
or maybe you want to associate a cost with each member of the hydration list
costs_hydration = [100,100,100]
costs_nutrition = [60,60,60]
costs = {}
costs.update(dict(zip(Hydration,costs_hydration)))
costs.update(dict(zip(Nutrition,costs_nutrition)))
print costs["Beans"]
Upvotes: 5