首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >业余爱好者:为什么我不能给这个字符串/变量一个硬币的值?

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

Stack Overflow用户
提问于 2014-10-09 02:10:46
回答 1查看 49关注 0票数 0
代码语言:javascript
复制
# 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)

我已经上传了我的全部代码,因为我猜我遗漏了一些你需要知道的部分。

我本打算把我遇到问题的部分用粗体表示出来,但这行不通。所以,

代码语言:javascript
复制
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

我正在尝试将变量Nutrition和Hydration赋值为60和100,这样程序就会知道从您的信用中减去多少,但它总是返回变量‘成本’没有定义。此外,代码似乎没有认识到产品(如上所示)是有效术语,尽管它们应该与变量Hydration和Nutrition相关联。

正如你所看到的,我对代码的艺术并不是很了解。这就是我需要帮助的原因。提前谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2014-10-09 02:13:45

你需要一本字典,而不是一份清单

代码语言:javascript
复制
cost ={}
cost["Hydration"] = 100
cost["Nutrition"] = 60
print cost["Nutrition"]

或者,您可能想要将成本与水合作用列表的每个成员相关联

代码语言:javascript
复制
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"]
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26263598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档