Python得分除零

时间:2016-02-26 02:22:35

标签: python-2.7 odoo-8

我尝试计算persentage,并显示错误浮点除以零,这是我的代码

    d4 = float(self.qty_normal)+float(self.qty_abnormal)
    d3 = float(self.qty_normal)+float(self.qty_recoveryabn)
    if self.flag_recovery == False:
        if self.qty_abnormal and self.qty_normal:
            pnormal =(self.qty_normal)/float(d3)*(100.00)
            pabnormal =(self.qty_abnormal)/float(d3)*(100.00)
            self.nursery_persentagea = pabnormal
            self.nursery_persentagen = pnormal
    if self.flag_recovery == True:
        if self.qty_recoveryabn and self.qty_normal:
            pnormal =(self.qty_normal)/float(d3)*(100.00)
            pabnormal =(self.qty_recoveryabn)/float(d3)*(100.00)
            self.nursery_persentagea = pabnormal
            self.nursery_persentagen = pnormal

帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

你有几个具有相同分母的表达式 - d3。分配后检查其值:

 d3 = float(self.qty_normal)+float(self.qty_recoveryabn)

它不应该为零,否则此表达式将无法评估:

pnormal =(self.qty_normal)/float(d3)*(100.00)

OR

pnormal =(self.qty_normal)/float(d3)*(100.00)

(取决于flag_recovery等。)