业余爱好者:为什么我不能给这个字符串/变量一个硬币值?

2024-05-19 17:07:18 发布

您现在位置:Python中文网/ 问答频道 /正文

# 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,这样程序就会知道从你的信用中扣除多少,但它不断返回变量“成本”没有定义。此外,代码似乎没有认识到产品(如上所示)是有效的条款,尽管事实上,他们应该连接到变量水合和营养。你知道吗

如你所见,我对代码的艺术不是很了解。所以我才需要帮助。先谢谢你。你知道吗


Tags: youinputnotmadamliketotalcoinprint
1条回答
网友
1楼 · 发布于 2024-05-19 17:07:18

你需要一本字典而不是一张单子

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

相关问题 更多 >