python简单百分比计算返回0

use*_*627 0 python math integer division python-2.x

好吧,这似乎是微不足道的,但我无法弄清楚这一点.我需要做一个基本的百分比计算.这是我的代码:

corr = 205
score =  100 * (corr / 225 )
print score 
Run Code Online (Sandbox Code Playgroud)

但结果是0.怎么会?没有完全不同的方法来写这个,但我确实改变了这个并尝试添加int()s和float()s,但结果总是如此0.

Ger*_*ano 5

你应该转换corr为a float,这是你意想不到的地方int

corr = 205
score =  100 * (float(corr) / 225)
print score
>>>  91.11111111111111
Run Code Online (Sandbox Code Playgroud)


Jin*_*Jin 5

添加到@Germano 的答案,

Python2.7

>>> corr = 205
>>> score = 100 * (corr / 225)
>>> print score
0
Run Code Online (Sandbox Code Playgroud)

Python3 修复了自动转换。

>>> corr = 205
>>> score = 100 * (corr / 225)
>>> print(score)
91.11111111111111
Run Code Online (Sandbox Code Playgroud)

请参阅如何强制除法为浮点数?除法不断向下舍入到 0