【问题标题】:Why is my answer not rounding to two decimal points using the Round function in python?为什么我的答案没有使用 python 中的 Round 函数四舍五入到小数点后两位?
【发布时间】:2021-12-15 04:23:28
【问题描述】:

#下面是我的代码。无法启动回合的代码有什么问题?我搜索了堆栈溢出,根据搜索结果似乎是正确的,但有些不对劲。我得到了正确的答案,但我的答案显示不正确,应该是 33.60,假设账单是 150,小费是 12,5 人分摊账单。

print("Welcome to the tip calculator!")
Bill_A = float(input("What was the total bill?"))
Tip_A = float(input("How much tip would you like to give? 10, 12, or 15?"))
Bill_Split = float(input("How many people to split the bill?"))
 
#Convert Tip to decimal for calculation.
Tip_B = (Tip_A / 100) + 1.00
 
Total_Bill = ((Bill_A / Bill_Split) * Tip_B)
 
Total_Bill2 = round(Total_Bill, 2)
 
print(f"Each person should pay: ${Total_Bill2}" )

【问题讨论】:

标签: python rounding


【解决方案1】:

我猜你想显示 33.60。如果是这样,请使用此代码。我已经使用 %.2f 设置了两位小数

print("Welcome to the tip calculator!")
Bill_A = float(input("What was the total bill?"))
Tip_A = float(input("How much tip would you like to give? 10, 12, or 15?"))
Bill_Split = float(input("How many people to split the bill?"))
 
#Convert Tip to decimal for calculation.
Tip_B = (Tip_A / 100) + 1.00
 
Total_Bill = ((Bill_A / Bill_Split) * Tip_B)
 
print(f"Each person should pay: %.2f" % Total_Bill )

【讨论】:

  • 谢谢!我正在努力理解 %.2f 的工作原理以及 total_bill 之前的 %。我想通过练习它会出现,但是有一个简单的解释来解释吗?为什么轮函数不起作用?
  • 是的,它们非常好用,一定要学会!
猜你喜欢
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 2013-12-25
  • 2014-06-03
相关资源
最近更新 更多