在python2中執行除法操作如果結果小於1就會返回0
如下面的例子:
>>>81/82 0
如果你需要返回"正確的結果 ",有兩種方法:
- 在腳本中引入from future import division
>>> from __future__ import division
>>> 81/82 0.98780487804878048
- 將除數或者被除數轉換為浮點數
>>> 81.0/82 0.98780487804878048 >>> 81/82.0 0.98780487804878048
在python3中不會出現這種問題:
如下面的例子:
Python 3.4.5 (default, Jun 1 2017, 13:52:39) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 81/82 0.9878048780487805