我正在写一个程序,输入2商品,应用25%折扣,并在总折扣商品价格上添加8.25%税率,但我总是得到错误的输出。
它可以处理某些数字,例如5 + 3,但不能正确处理8.75 + 25.99。
first_item = int(float(input("Price of 1st item: ")))
second_item = int(float(input("Price of 2nd item: ")))
discount = (first_item + second_item) *.25
discounted_items = (first_item + second_item) - discount
tax = discounted_items *.0825
final_pay = discounted_items + tax
print("You pay ", final_pay)8.75 + 25.99的预期结果是28.204537,但我一直得到26.7918。
发布于 2019-09-11 05:23:51
将输入从str转换为float很好,但当您再次将其转换为int时,会丢失小数部分(.xxx),因此会丢失精度。3和5没有出现此问题,因为它们已经是整数,因此不会丢失任何内容,因此只需保留输入float即可
first_item = float(input("Price of 1st item: "))
second_item = float(input("Price of 2nd item: "))https://stackoverflow.com/questions/57878325
复制相似问题