Python提示计算器错误

Python提示计算器错误,python,Python,下面的代码应该可以顺利运行,但由于某种原因,终端告诉我它有问题。我的问题在下面 print 'Welcome to Cash Calculator!' cash = input('How much was the original price of the services or goods you paid for, excluding vat?') tip = input('How much more, as a percentage, would you like to give as

下面的代码应该可以顺利运行,但由于某种原因,终端告诉我它有问题。我的问题在下面

print 'Welcome to Cash Calculator!'

cash = input('How much was the original price of the services or goods you paid for, excluding vat?')
tip = input('How much more, as a percentage, would you like to give as a tip?')

tip = tip/100

print tip

vat = 1.2

cash_vat = cash * vat

can = (cash_vat + ((tip/100) * cash_vat))

can = cash_vat + tip * cash_vat

print """
Thank you for your co-operation.
The price excluding the tip is %r,
and the total price is %d.
"""  % (cash_vat, can)
当上述代码运行时,终端发出:

Welcome to Cash Calculator!
How much was the original price of the services or goods you paid for, excluding vat?100
How much more, as a percentage, would you like to give as a tip?10
0

Thank you for your co-operation.
The price excluding the tip is 120.0,
and the total price is 120.
有什么问题吗?它一直认为小费是0。我是一个完全的初学者。

如果分子和分母都是整数,则除法运算符/执行整数除法,在本例中,它们是整数,因为您使用了输入。因此,行动:

# if tip = 10
tip = 10/100
将返回0,因为这两个值的类型都是int

由于需要浮点除法,您可以从模块中导入除法运算符:

或者,在实际分割之前,将int类型的尖端强制转换为浮点:

tip = float(10) / 100 # returns 0.1

@MorganThrapp它不是关于读取整数,而是关于整数除法。@bereal输入返回一个str。OP执行100/100操作,因此出现错误。@Leb它是Python2,输入求值字符串。否则它将是TypeError。@bereal你是对的,原始输入将被修复。@RichardDickins:如果这回答了你的问题,你会接受它吗?为此,请单击答案左侧的勾号-这就是我们如何将问题标记为已解决,将答案标记为正确的方法。这也是一个很好的方式来认可海报的努力,因为它给了他们一些额外的声誉点。
tip = float(10) / 100 # returns 0.1