mardi 4 août 2015

python ZeroDivisionError: float division by zero - how to treat it as an exception

I need to calculate the percentage of winning rate and I am having trouble with ZeroDivisionError: float division by zero when there is zero in the denominator. I would like treat it as an exception, print zero and move on to next array. Any assistance would be appreciated.

YearWinLose1 = '0'
YearWinLoseTotalHard2 = '0'

YearWinLoseTotalHard2 = float(YearWinLose1) + float(YearWinLose1)
YearWinLosePercent3 = float(YearWinLose1)/float(YearWinLoseTotalHard2)*100

if YearWinLoseTotalHard2 == 0:
   YearWinLosePercent3 == 0
   print YearWinLosePercent3
else: 
   print YearWinLosePercent3



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire