此代码需要允许最多3个输入所需的项目,并打印所有项目的总成本。我对这一切都很陌生,需要我能得到的所有建议。我无法将总数打印出来。
pie = 2.75
coffee = 1.50
icecream = 2.00
while choice:
choice01 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
if choice01 == "nothing":
break
choice02 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
if choice02 == "nothing":
break
choice03 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
if choice03 == "nothing":
break
cost = choice01+choice02+choice03
print "Your total is: ${0}" .format(cost)
答案 0 :(得分:3)
让我们关注您的代码正在做什么。
choice01 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
当用户回答此问题时,他们的回答是一个字符串。它现在在choice01
。对于这个例子,让我们假设他们键入" pie" (没有引号)。
我们使用choice02 =
行重复此操作,用户选择"咖啡"。
让我们看看你的cost =
行只有这两个选择。
cost = choice01 + choice02
我们刚刚确定choice01
是字符串值" pie"而choice02
是字符串值" coffee"因此,cost = "piecoffee"
您想在顶部使用这些变量。一种方法是创建一个字典:
prices = {"pie": 2.75,
"coffee": 1.50,
"icecream": 2.00,
"nothing": 0.00
}
...
cost = prices[choice01]+prices[choice02]+prices[choice03]
在字典中,我设置了4个可能的值和相关价格。 "无"值为0.00,因为您在成本计算中使用了它。它使得数学变得简单而且有效,因为你假设总会有3个选择。
重要的是要注意,使用这种方法,如果用户输错了他们的答案(即" cofee"而不是"咖啡"),它将抛出异常。这是一项活动,您可以确定如何处理此类错误。您可以添加这样一个检查点。
还有一些其他问题需要解决:
while choice:
不会像您的代码一样开始工作。你没有定义choice
。另一种方法是简单地执行while True
,然后在循环结束时突破。 示例:
prices = {"pie": 2.75,
"coffee": 1.50,
"icecream": 2.00
}
cost = 0.00
while True:
choice = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
if choice == "nothing":
break
cost = cost + prices[choice]
print "Your total is: ${0}" .format(cost)
输出:
What would you like to buy? pie, coffee, icecream, or nothing? pie
What would you like to buy? pie, coffee, icecream, or nothing? nothing
Your total is: $2.75
请注意,我们只有一次用户输入问题,我们在循环开始之前定义cost
。然后我们每次都通过循环添加。我也删除了"没有"字典中的键,因为在将选择添加到成本之前,您会跳出循环。
答案 1 :(得分:1)
{{1}}
{{1}}
答案 2 :(得分:0)
您在顶部定义的是馅饼,咖啡和冰淇淋的变量名称。
您从raw_input获得的是文本字符串。
它们不匹配只是相同,你必须以某种方式匹配它们。我建议更像:
pie = 2.75
coffee = 1.50
icecream = 2.00
cost = 0
while True:
choice = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
if choice == "nothing":
break
if choice == "pie":
cost = cost + pie
if choice = "coffee":
cost = cost + coffee
#... etc.
print "Your total is: ${0}".format(cost)
如果你想避免大量的if
陈述,请查看@Evert关于持有价格的字典的建议。
答案 3 :(得分:0)
看来你根本不需要三个选择。请考虑以下代码
pie = 2.75
coffee = 1.50
icecream = 2.00
cost = 0
while True:
choice01 = 0
choice01 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
if choice01 == "nothing":
break
elif choice01=="coffee":
choice01 = coffee
elif choice01=="pie":
choice01 = pie
elif choice01=="icecream":
choice01==icecream
else:
choice01=0
cost+=choice01
print "Your total is: ${0}" .format(cost)
答案 4 :(得分:0)
确保用户只输入饼而不是" pie"因为Python会采用" pie"作为一个字符串。
考虑使用字典: - choices = {'pie' : 2.75, 'coffee':1.50, 'icecream': 2.00, 'nothing':0}
然后使用for循环。您也可以使用文件循环,但如果没有必要,建议使用更简单的循环。
在启动循环之前也声明cost= 0
。这是循环: -
for x in range(0, 3):
choosed = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
cost = cost + choices[choosed]
然后最后(外部循环)用户print "Your total is: ${0}" .format(cost)
获得总金额。
将所有这些放在一起: -
choices = {'pie' :2.75, 'coffee':1.50, 'icecream': 2.00, 'nothing': 0}
cost = 0
for x in range(0, 3):
choosed = input("What would you like to buy? pie, coffee, icecream, or nothing?")
cost = cost + choices[choosed]
print "Your total is: ${0}" .format(cost)
P.S。: - 我建议而不是要求输入3次,一旦用户输入EOF就停止询问。为此,您可以考虑if choosed == "nothing": break