Search code examples
python-2.7odoo-8

Python getting division by zero


i try to calculate persentage , and show error float division by zero , and this my code

    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

help me to solve this


Solution

  • You have several expressions which have the same denominator - d3. Check its value after the assignment:

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

    It should not be zero, otherwise this expression will fail to evaluate:

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

    OR

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

    (depends on flag_recovery etc.)