Select to view content in your preferred language

ZeroDivisionError:

1849
2
Jump to solution
01-13-2014 07:08 AM
ClintonCooper1
Deactivated User
I have a large dataset where I need to run a lot of calculations.  it contains roughly 100 fields.  I have created a script that runs an update cursor to calculate some new fields based on a few other fields.  Well I am running into the problem where if there is a zero in the field that is a divisor, I get this error: 

Runtime error  Traceback (most recent call last):   File "<string>", line 36, in <module> ZeroDivisionError: float division by zero


I am looking for how to construct an if statement that will first determine if the calculation will create this error, then if it does, it will make the value 0

my first try of this:
if ((row[32]/row[3])/(row[30]/row[3]) =0):    row[84]= (row[32]/row[3])/(row[30]/row[3]) else:   row[84]= 0


did not work.  Any help on how to make this if statement work would be much appreciated!!!  Thanks in advance!!

Clinton
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
You can't test for something that will give you a division error. I imagine you tried this.
if ((row[32]/row[3])/(row[30]/row[3]) != 0):

Will give you an error just for testing it if it includes zero division errors, so doesn't serve any purpose. All you need to check is if the denominator(s) are zero.

if row[3] == 0:     row[84] = 0

View solution in original post

0 Kudos
2 Replies
MathewCoyle
Honored Contributor
You can't test for something that will give you a division error. I imagine you tried this.
if ((row[32]/row[3])/(row[30]/row[3]) != 0):

Will give you an error just for testing it if it includes zero division errors, so doesn't serve any purpose. All you need to check is if the denominator(s) are zero.

if row[3] == 0:     row[84] = 0
0 Kudos
ClintonCooper1
Deactivated User
ahh yep!  Thank you!  It worked!

Clinton
0 Kudos