Python:除法错误

Kei*_*ong 0 python

继承我的方法:

def geometricSum(commonRatio, firstTerm, lastTerm):
    return ((firstTerm - lastTerm) * commonRatio) / (1 - commonRatio)
Run Code Online (Sandbox Code Playgroud)

口译员测试:

    >>> geometricSum(1.0,1.0,100.0)
    Traceback (most recent call last):
      File "<interactive input>", line 1, in <module>
        File "F:\PortablePython_1.1_py3.0.1\sumOfGeometric.py", line 2, in geometricSum
          return ((firstTerm-lastTerm)*commonRatio)/(1-commonRatio)
    ZeroDivisionError: float division
Run Code Online (Sandbox Code Playgroud)

Rus*_*ove 10

你除以零.commonRatio是1.0,因此分母是1-1.0 = 0.


sle*_*ica 9

哈哈,我很抱歉直言不讳,但这与双打毫无关系.你是由ZERO分开的!如果不是python的异常处理,你可能会炸毁宇宙!

  • +1:只有查克诺里斯可以除以零. (6认同)
  • @sdolan:我不知道,我听说Jon Skeet也可以除以零. (2认同)

vla*_*adv 7

1 - commonRatio = 1 - 1 = 0.
Run Code Online (Sandbox Code Playgroud)

python中尚不支持除以0.或者数学.