ZeroDivisionError:即使我有一个零捕手,浮点除零

0 python integer-division divide-by-zero floating

我对Python有点新鲜.我在下面附上了一段代码.constant_a&b是整数.运行此代码时,我收到以下错误:

回溯(最近一次调用最后一次):文件"U:\ V10_run2\process.py",第209行,delta_mcs_2_gfx_percentage =(delta_mcs_2_gfx*100)/ float(mcs)ZeroDivisionError:浮点除零

mcs=hash["MCF"]*constant_a/constant_b  

if mcs is 0:
      delta__percentage=-100
else:
      delta__percentage=(delta*100)/mcs
Run Code Online (Sandbox Code Playgroud)

正如错误所说,我认为这是因为python试图进行整数除法float(delta*100)/float(mcs)并将mcs舍入为0但我也尝试过这也没有帮助.有什么建议 ??

and*_*ece 6

尝试使用==而不是is:

a = 0.0

if a is 0:
    print("is zero")
else:
    print("not zero")
# 'not zero'

if a == 0:
    print("== zero")
else:
    print("not zero")
# '== zero'
Run Code Online (Sandbox Code Playgroud)

有关更多解释,请参阅此文章.本质上,==测试相等性和is测试精确对象身份.