I am calculating a field based on two other fields in ArcMap 10.2 using python.

My Pre-Logic Script Code is

def myfunc(legend,Btype):

if(legend == 'Apple'):
    return 'Banana'
elif(legend == 'Cranberry'):
    return 'Date'
elif(legend == 'Elderberry'):
        return 'Grape'
        return 'Mango'
    elif Btype is None:
        return 'Olive'
        return 'Pear'
    return 'Raspberry'

Mostly this function works, however in the case that legend is "Elderberry" and Btype is "Null" the function returns "Null" rather than "Olive"

The problem only occurs when I nest the "is None" in the if statement.

1 Answer 1


Move your "is None" check up to the first evaluation.

if Btype is None:
  return 'Olive'
elif (Btype.startswith('Fig')):
  return 'Grape'
  return 'Mango'

Basically, if Btype is null, you can't do .startswith, etc. on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.