I am trying to calculate a field based on the values of 3 other fields. 2 are text and 1 is defined as double. When I run the following code only using the 2 text fields everything works as expected:
myCalc(!ASSIGNED!,!DEFAULTED!)
def myCalc(ASSIGNED,DEFAULTED,):
if (ASSIGNED=='YES')and(DEFAULTED=='NO'):
return 'NO'
elif (ASSIGNED=='YES')and(DEFAULTED=='YES'):
return 'YES'
elif (ASSIGNED=='NO')and(DEFAULTED=='YES'):
return 'YES'
elif (ASSIGNED=='NO')and(DEFAULTED=='NO'):
return 'YES'
else:
return 'INCONCLUSIVE'
However when I try to assess against the 3rd field most of the statements get ignored and only a few are implemented as expected. No errors or warnings are generated. This results in most of the results coming in as Inconclusive as generated by the final statement. Given the data I am using no result should come in as inconclusive since no value is blank or null. The logic for assessing the 3rd field is as follows:
def myCalc(ASSIGNED,DEFAULTED,DISTANCE,):
if (ASSIGNED=='YES')and(DEFAULTED=='NO'):
return 'NO'
elif (ASSIGNED=='YES')and(DEFAULTED=='YES')and(DISTANCE<21):
return 'YES'
elif (ASSIGNED=='YES')and(DEFAULTED=='YES')and(DISTANCE>=21):
return 'NO'
elif (ASSIGNED=='NO')and(DEFAULTED=='YES')and(DISTANCE<21):
return 'YES'
elif (ASSIGNED=='NO')and(DEFAULTED=='YES')and(DISTANCE>=21):
return 'NO'
elif (ASSIGNED=='NO')and(DEFAULTED=='NO')and(DISTANCE<21):
return 'YES'
elif (ASSIGNED=='NO')and(DEFAULTED=='NO')and(DISTANCE>=21):
return 'NO'
else:
return 'INCONCLUSIVE'
If the value for distance is placed in single quotes I get error 000539. Any help is greatly appreciated.
Solved! Go to Solution.
Thanks Ken, I shut down ArcPro, reopened the program and repasted the code and its now working. No Idea what the issue was but restarting seems to have cleared it.
Can you tell which statements are being correctly calculated? And are you including the distance field in the myCalc initialization?
myCalc(!ASSIGNED!,!DEFAULTED!,!DISTANCE!)
And when adding code (especially Python, where indentation is important) use the Code Syntax tool.
Thank you for the Code Syntax information.
Working Code:
myCalc(!ASSIGNED!,!DEFAULTED!)
def myCalc(ASSIGNED,DEFAULTED):
if (ASSIGNED=='YES')and(DEFAULTED=='NO'):
return 'NO'
elif (ASSIGNED=='YES')and(DEFAULTED=='YES'):
return 'YES'
elif (ASSIGNED=='NO')and(DEFAULTED=='YES'):
return 'YES'
elif (ASSIGNED=='NO')and(DEFAULTED=='NO'):
return 'YES'
else:
return 'INCONCLUSIVE'
Broken Code:
myCalc(!ASSIGNED!,!DEFAULTED!,!DISTANCE!)
def myCalc(ASSIGNED,DEFAULTED,DISTANCE,):
if (ASSIGNED=='YES')and(DEFAULTED=='NO'):
return 'NO'
elif (ASSIGNED=='YES')and(DEFAULTED=='YES')and(DISTANCE<21):
return 'YES'
elif (ASSIGNED=='YES')and(DEFAULTED=='YES')and(DISTANCE>=21):
return 'NO'
elif (ASSIGNED=='NO')and(DEFAULTED=='YES')and(DISTANCE<21):
return 'YES'
elif (ASSIGNED=='NO')and(DEFAULTED=='YES')and(DISTANCE>=21):
return 'NO'
elif (ASSIGNED=='NO')and(DEFAULTED=='NO')and(DISTANCE<21):
return 'YES'
elif (ASSIGNED=='NO')and(DEFAULTED=='NO')and(DISTANCE>=21):
return 'NO'
else:
return 'INCONCLUSIVE'
After looking at the results more closely only the 5th elif seems to be returning a yes result, but it is ignoring the distance portion of the statement thus returning yes for distances over 21. Hopefully this helps.
I ran your code on a test file and it returned all the expected results. I used a different return on if to make sure they were all being evaluated correctly.
Thanks Ken, I shut down ArcPro, reopened the program and repasted the code and its now working. No Idea what the issue was but restarting seems to have cleared it.
def myCalc(ASSIGNED, DEFAULTED, DISTANCE=None):
if ASSIGNED == 'YES':
if DEFAULTED == 'YES':
if DISTANCE < 21:
return 'YES'
return 'NO'
return 'SOMETHING'
elif ASSIGNED=='NO':
if DEFAULTED == 'YES':
if DISTANCE < 21:
return 'YES'
return 'NO'
else:
if DISTANCE < 21:
return 'YES'
return 'NO'
else:
return 'INCONCLUSIVE'