【问题标题】:I'm having trouble running this program [closed]我在运行此程序时遇到问题[关闭]
【发布时间】:2017-12-07 00:45:36
【问题描述】:
# Tipper Program
# User enters the bill total and the program computes two amounts,
# a 15 percent tip and a 20 percent tip

print("\t\t\t **  **  **  **  **  **  **  **  **  **")

print("\n\t\t\t\t    Tip Calculator")

print("\n\t\t\t **  **  **  **  **  **  **  **  **  **")

bill = input("\nPlease enter your restaurant's bill total: ")

tip_15 = bill * 0.15

tip_20 = bill * 0.20

float(print("\n\nA 15% tip for this bill comes out to ", tip_15))
float(print("\nA 20% tip for this bill comes out to ", tip_20))


input("\n\nPress the enter key to exit.")

在我输入账单总额后,应用程序一直关闭。有解决此问题的建议吗?

【问题讨论】:

  • 您将打印语句转换为浮点数?从倒数第二行和第三行删除float()
  • 一般提示:从cmd.exe 运行程序(假设是Windows)。如果您只是双击它,它会在出现错误时立即退出,并且您将无法阅读有用的错误消息。
  • @Blorgbeard:你错过了最后的input("\n\nPress the enter key to exit.")吗?
  • @martineau 如果在该行之前发生错误则不会执行

标签: python python-3.x input casting user-input


【解决方案1】:

当您要求用户输入时,您需要将此值转换为浮点数,然后再将其乘以 0.15 和 0.20

bill = float(input("\nPlease enter your restaurant's bill total: "))

你的最后两行也不应该转换成浮点数

print("\n\nA 15% tip for this bill comes out to: ", tip_15)
print("\nA 20% tip for this bill comes out to: ", tip_20)

【讨论】:

  • 可能想做的事:print("\n\n这个账单的 15% 小费来自 ".format(tip_15))
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 2022-10-16
  • 2021-05-23
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多