2014-10-08 37 views
0
# 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) 

我已經上傳了我的全部代碼,因爲我猜那裏會是我離開了,你需要知道的位。這裏業餘愛好者:爲什麼我不能給這個字符串/變量一個硬幣值?

我會嘗試把我在用粗體的問題位,但沒有奏效。所以,

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 

我試圖將變量分配的營養和水分爲60和100的值,所以該方案將知道多少從您的信用卡拿走,但它一直返回該變量「成本' 沒有定義。此外,代碼似乎沒有認識到產品(如上所示)是有效的條款,儘管它們應該連接到變量水合和營養。

正如你所看到的,我不是藝術的代碼非常熟悉。這就是爲什麼我需要幫助。先謝謝你。

回答

5

,你需要一本字典不是一個列表

cost ={} 
cost["Hydration"] = 100 
cost["Nutrition"] = 60 
print cost["Nutrition"] 

或者你想將成本與水化列表中的每個成員

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"] 
+0

多謝關聯,人 - 它的工作。但是,該計劃不承認營養和水合作用中的物品(食品)。輸入時,它只顯示我的'無效期限'通知。我不明白爲什麼,如果你可以得到一個非常方便的幫助。謝謝。 – LuxAffinity 2014-10-09 15:32:02

+0

你需要使用字符串而不是變量'而pro1不在('水合','營養'):' – 2014-10-09 16:03:19

相關問題