我在python代码中犯了什么错误,它赢了';不打印总成本吗?
此代码需要为所需的项目允许最多3个输入,并打印所有项目的总成本。我对这一切都很陌生,需要所有能得到的建议。我无法把总数打印出来我在python代码中犯了什么错误,它赢了';不打印总成本吗?,python,scripting,Python,Scripting,此代码需要为所需的项目允许最多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":
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)
您在顶部定义的是馅饼、咖啡和冰淇淋的可变名称 从原始输入中得到的是文本字符串 他们不匹配只是因为他们是一样的,你必须以某种方式匹配他们。我建议更像:
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的建议,即建立一个包含价格的词典。你在顶部定义的是馅饼、咖啡和冰淇淋的变量名称
从原始输入中得到的是文本字符串
他们不匹配只是因为他们是一样的,你必须以某种方式匹配他们。我建议更像:
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的建议,即使用一本字典来保存价格。似乎你根本不需要三种选择。考虑下面的代码
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)
看来你根本不需要三个选择。考虑下面的代码
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)
让我们关注您的代码正在做什么
choice01 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
当用户回答这个问题时,他们的答案是字符串。它现在位于选项01
中。对于本例,假设他们键入“pie”(无引号)
我们用choice02=
行重复此操作,用户选择“coffee”
让我们看看您的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个可能的值和相关价格。“nothing”的值为0.00,因为您在成本计算中使用了该值。它使数学变得简单,而且它是有效的,因为你假设总是会有3个选择 需要注意的是,使用此方法时,如果用户键入了他们的答案(即“cofee”而不是“coffee”),它将抛出异常。这是一项让您决定如何处理此类错误的活动。您可以添加这样的检查,这有很多方面
其他修复 您还需要解决一些其他问题:
不会像您的代码那样工作。您没有定义while choice:
。另一种方法是,在为True时执行选项
,然后在循环结束时中断李>
- 您可以将整个输入循环压缩为一个简单的ask,并添加到运行总数中。这将允许您选择3个以上的选项
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
。然后,每次通过循环,我们只需将其添加。我还从字典中删除了“nothing”键,因为在将选择添加到成本之前,您打破了循环。让我们关注一下您的代码在做什么
choice01 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")
当用户回答这个问题时,他们的答案是字符串。它现在位于选项01
中。对于本例,假设他们键入“pie”(无引号)
我们用choice02=
行重复此操作,用户选择“coffee”
让我们看看您的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个可能的值和相关价格。“nothing”的值为0.00,因为您在成本计算中使用了该值。它使数学变得简单,而且它是有效的,因为你假设总是会有3个选择 需要注意的是,使用此方法时,如果用户键入了他们的答案(即“cofee”而不是“coffee”),它将抛出异常。这是一项让您决定如何处理此类错误的活动。您可以添加这样的检查,这有很多方面
其他修复 您还需要解决一些其他问题:
不会像您的代码那样工作。您没有定义while choice:
。另一种方法是,在为True时执行选项
,然后在循环结束时中断李>
- 您可以将整个输入循环压缩为一个简单的ask,并添加到运行总数中。这将允许您选择3个以上的选项
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
。然后,每次通过循环,我们只需将其添加。我还从字典中删除了“nothing”键,因为您在将选择添加到成本之前打破了循环
price\u dict=dict(
pie=2.75,
咖啡=1.50,
冰淇淋=2.00,
无=无)
price\u dict=dict(
pie=2.75,
咖啡=1.50,
冰淇淋=2.00,
无=无)
choices={'pie':2.75,'coffee':1.50,'icecream':2.00,'nothing':0}
然后使用for循环。你可以帮助我们