我是python的初学者,我想知道我设置的这个计算tips的程序有什么问题
total = input("What is the bill total? ")
tperc = input("Would you like to give a 15% or 20% tip? ")
tip15 = total * .15
tip20 = total * .20
if tperc == "15":
print("\nThat would be a $" + tip15 + "tip.")
if tperc == "15%":
print("\nThat would be a $" + tip15 + "tip.")
if tperc == "20":
print("\nThat would be a $" + tip20 + "tip.")
if tperc == "20%":
print("\nThat would be a $" + tip20 + "tip.")
input("\nPress enter to exit.")
谢谢你的帮助
在python3.X中,
input
返回一个字符串。不能将字符串与浮点数相乘。你知道吗在做任何算术之前把
total
转换成一个数字。你知道吗或者,在理想情况下,使用十进制类型进行货币计算,因为浮点运算往往是not perfectly accurate。你知道吗
这也是一个问题,因为不能将字符串和浮点/十进制连接起来。转换为字符串或使用字符串格式。后者可能更可取,因为您可以四舍五入到小数点后两位。你知道吗
相关问题 更多 >
编程相关推荐