【问题标题】:How can I get my Tipper program done right?如何正确完成我的自卸车程序?
【发布时间】:2022-01-24 18:10:16
【问题描述】:

我目前正在为这项工作编写一个 Tipper 程序,但我遇到了很多问题。结果一直说语法无效,我已经切换了很多东西,但似乎我永远无法让它恰到好处。举个例子,这是我当前的代码:

print("Now that your belly is full, how much is the fee?")
fee = input("Enter your fee: ")
print("Would you like to give us a tip?")
answer = input("Yes or no?")
if answer == no:
    why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
    print("We understand. Here is your fee:", fee)
elif answer == yes:
    tip = input("Would you like to tip 15 percent or 20 percent? ")
    if tip == 15 :
        cost1 = fee/15 + fee
    elif tip == 20 :
            cost2 = fee/20 + fee
    else:
        print("I do not understand.")
else:
    print("I do not understand.")
print:("Have a wonderful day!")

所以,是的,老实说,我不确定为什么它不起作用,但有人可以回答吗?我真的被这件事难住了。

【问题讨论】:

  • if answer == no: ...你提到if answer == "no": ?
  • tip = input("Would you like to tip 15 percent or 20 percent? ") if tip == 15 : - tip 是一个字符串,绝不是 15。
  • 为什么我需要 tilp 15 或 20 - 也许我只想给 8% ... 或固定金额 ....
  • 其中可能还有很多其他错误......也许你应该在生产生产代码之前学习how to debug small programs
  • 好的,那么我应该在 15 和小费之间放什么?

标签: python python-3.x


【解决方案1】:

我修复了您代码中的错误并对其进行了测试,但整体代码结构仍有待改进。

那么是什么导致你的程序崩溃了呢?

  • “No”和“Yes”都是字符串,所以你必须把它们放在“”之间
  • 您必须使用int() 函数将用户输入的费用转换为整数,以便对其进行数学计算。
  • 您计算的小费错误,我使用这些计算修复了它 15% >> 总计 = 1.15xfee
    20% >> 总计 = 1.20xfee
    注意:在下面的代码中,我使用了math.ceil() 函数将小费四舍五入为整数。
    为了稍微改善用户体验,我还添加了一行打印总成本(包括小费)返回给用户。 这是更正后的代码:
import math

print("Now that your belly is full, how much is the fee?")
fee = int(input("Enter your fee: "))
print("Would you like to give us a tip?")
answer = input("yes or no?")
if answer == "no":
    why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
    print("We understand. Here is your fee:", fee)
elif answer == "yes":
    tip = int(input("Would you like to tip 15 percent or 20 percent? "))
    if tip == 15 :
        cost = math.ceil(1.15 * fee)
        print("Your total, including fee is, ", cost)
    elif tip == 20 :
            cost = math.ceil(1.20 * fee)
            print("Your total, including fee is, ", cost)
    else:
        print("I do not understand.")
else:
    print("I do not understand.")
print("Have a wonderful day!")

【讨论】:

  • 感谢您提供的信息。
  • 不客气,祝你好运
【解决方案2】:

正如 cmets 中所述,您需要将要测试的答案用引号括起来(双引号 " 或单引号 ' 可以),否则 python 会认为它们是变量名而不是字符串文字,您要与之进行比较。

另外,在最后的打印中,您有一个不属于那里的冒号 (:)。

这是修复了这两个问题的代码。

print("Now that your belly is full, how much is the fee?")
fee = input("Enter your fee: ")
print("Would you like to give us a tip?")
answer = input("Yes or no?")
if answer == 'no':
    why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
    print("We understand. Here is your fee:", fee)
elif answer == 'yes':
    tip = input("Would you like to tip 15 percent or 20 percent? ")
    if tip == 15:
        cost1 = fee / 15 + fee
    elif tip == 20:
        cost2 = fee / 20 + fee
    else:
        print("I do not understand.")
else:
    print("I do not understand.")
print("Have a wonderful day!")

此外,这种硬编码选项和跳过问题的简单方法(例如一旦回答“是”)会导致非常令人沮丧的用户体验,但是这是另一个问题。

编辑: 您还将字符串与数字进行比较,这始终是错误的,这使您免于错误,即尝试将字符串除以数字会产生

【讨论】:

  • 你的代码还是错的。 input 返回一个字符串,所以tip 永远不会等于1520,而fee 也是一个字符串。
  • 你说得对,我刚看到就想回来提一下^^
【解决方案3】:

这是一个有效的版本。现在,仍然存在问题。首先,如果他们输入的垃圾不是账单或小费的数字,它不会被捕获。其次,它不圆小费,因为我不知道你使用的是什么货币。但至少你可以运行它并得到答案。

print("Now that your belly is full, how much is the bill?")
fee = float(input("Enter your bill: "))
print("Would you like to give us a tip?")
answer = input("yes or no?")
if answer.lower() == 'no':
    why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
    print("We understand.")
elif answer.lower() == "yes":
    tip = int(input("What tip percentage do you use? "))
    fee += fee * tip / 100
else:
    print("I do not understand.")
print("Your total bill is", fee)

【讨论】:

  • 感谢您的信息。
猜你喜欢
  • 2011-03-16
  • 1970-01-01
  • 2023-02-10
  • 2012-06-15
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多