Calculate Field Logic Ignoring Some Statements

577
5
Jump to solution
09-08-2023 12:44 PM
Labels (2)
FlightDeck
New Contributor III

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.

0 Kudos
1 Solution

Accepted Solutions
FlightDeck
New Contributor III

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. 

View solution in original post

0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor

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.

0 Kudos
FlightDeck
New Contributor III

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. 

 

0 Kudos
KenBuja
MVP Esteemed Contributor

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.

python.png

0 Kudos
FlightDeck
New Contributor III

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. 

0 Kudos
DanPatterson
MVP Esteemed Contributor
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'

... sort of retired...
0 Kudos