繁体   English   中英

我在我的代码python中做错了什么,它不会打印出全部费用?

[英]What am I doing wrong in my code python that it won't print the totall cost?

此代码最多需要允许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)

让我们专注于代码在做什么。

choice01 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")

当用户回答这个问题时,他们的答案就是一个字符串。 现在处于选择choice01 对于此示例,假设它们键入“ pie”(不带引号)。

我们使用choice02 = line重复此操作,然后用户选择“咖啡”。

让我们看一下这两个选择的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”而不是“ coffee”),它将抛出异常。 这是确定您如何处理此类错误的活动。 您可以在很多方面添加这样的检查。


其他修复

您还需要解决其他一些问题:

  • while choice:将无法按照您的代码立场行事。 您没有定义choice 另一种选择是简单地while True ,然后在循环结束时中断。
  • 您可以将整个输入循环压缩为一个简单的询问,然后添加到运行总计中。 这样一来,您可以选择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”键,因为您在将选择添加到成本之前就跳出了循环。

  1. 您应该检查输入内容,这可能超出您的选择范围
  2. , 您使用未定义的变量:

price_dict = dict( pie=2.75, coffee=1.50, icecream=2.00, nothing=None)

max_choices = 3
cost = 0

while max_choices:
    choice = raw_input(
        "What would you like to buy? pie, coffee, icecream, or nothing?")
    # please check the input
    if choice not in price_dict:
        print 'Your input is invalid, please enter again...'
        continue
    if choice == 'nothing':
        break
    max_choices -= 1
    cost += price_dict[choice]

print "Your total is: ${0}" .format(cost)

您在顶部定义的是派,咖啡和冰淇淋的变量名。

从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建议使用包含价格的字典。

看来您根本不需要三个选择。 考虑以下代码

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)
  1. 确保用户仅输入pie而不输入“ pie”,因为Python会将“ pie”作为字符串。

  2. 考虑使用字典: choices = {'pie' : 2.75, 'coffee':1.50, 'icecream': 2.00, 'nothing':0} ,然后使用for循环。 您也可以使用文件循环,但是如果不需要,建议使用更简单的循环。

  3. 还要在启动循环之前声明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)

PS:-我建议您不要询问3次输入,而是在用户输入EOF后停止询问。 为此,您可以考虑if choosed == "nothing": break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM