How do I handle divide by zero using try except in Python
op_dictNns = dict(result2.compute().values)
dictNns = {k: NNS1(op_dictNns[k], v, float(1-(op_dictNns[k]/v)), float((1-(op_dictNns[k]/v)) * 0.15)) for k, v in dict(dataNns.values).items()}
op_dict = dict(result.compute().values)
dict2 = {k: Summary(op_dict[k], v, float(op_dict[k] / v), float(op_dict[k] / v) * 0.70) for k, v in dict(data.values).items()}
op_dict3 = dict(result1.compute().values)
dict3 = {k: Mandate(op_dict3[k], v, float(op_dict3[k]/op_dict[k]), float(op_dict3[k]/op_dict[k]) * 0.15) for k, v in dict(data.values).items()}
CodePudding user response:
With ZeroDivisionError
Exception :
try:
print(12/0)
except ZeroDivisionError:
print("Can't divide by zero !")
#ouput :
#Can't divide by zero !
CodePudding user response:
try:
op_dict = dict(result.compute().values)
dict2 = {k: Summary(op_dict[k], v, float(op_dict[k] / v), float(op_dict[k] / v) * 0.70) for k, v in dict(data.values).items()}
op_dict3 = dict(result1.compute().values)
dict3 = {k: Mandate(op_dict3[k], v, float(op_dict3[k]/op_dict[k]), float(op_dict3[k]/op_dict[k]) * 0.15) for k, v in dict(data.values).items()}
except ZeroDivisionError:
print('ZeroDivisionError')```