1

I'm attempting to debug a code for a conditional label expression. The code is based on one i have previously used and did not have any issues. The python code is as follows.

def FindLabel ( [TF], [INV_1], [DIR_1], [INV_2], [DIR_2], [INV_3], [DIR_3], [INV_4], [DIR_4], [INV_5], [DIR_5], [INV_6], [DIR_6] ):
 return def FindLabel ( [TF], [INV_1], [DIR_1], [INV_2], [DIR_2], [INV_3], [DIR_3], [INV_4], [DIR_4], [INV_5], [DIR_5], [INV_6], [DIR_6]):
 myLabel = "TF {0}".format([TF])
 if not [INV_1] is None:
myLabel += "\nINV {0}".format([INV_1]) & [DIR_1]
 if not [INV_2] is None:
myLabel += "\nINV {0}".format([INV_2]) & [DIR_2]
 if not [INV_3] is None:
myLabel += "\nINV {0}".format([INV_3]) & [DIR_3]
 if not [INV_4] is None:
myLabel += "\nINV {0}".format([INV_4]) & [DIR_4]
 if not [INV_5] is None:
myLabel += "\nINV {0}".format([INV_5]) & [DIR_5]
 if not [INV_6] is None:
myLabel += "\nINV {0}".format([INV_6]) & [DIR_6]
 return myLabel

Where TF, INV_1, INV_2, INV_3, INV_4, INV_5, and INV_6 are numbers, and DIR_1, DIR_2, DIR_3, DIR_4, DIR_5, and DIR_6 are text, all have the potential to be null. When I click "OK" arcgis returns the following error

The expression contains an error. 
Modify the expression and try again. 
Error 0 on line 0. 
Syntaxerror: invalid syntax (<string>, line 2).

Coding is far from my strong suit.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 24, 2017 at 14:47
0

1 Answer 1

1

There are a number of issues in your code. The error message is referring to the first issue it found - if you that issue you'd get an error for the next. I'll try to outline them all for you here (hopefully I don't miss any).

  1. Your second line shouldn't be there. Remove this line

    return def FindLabel ( [TF], [INV_1], [DIR_1], [INV_2], [DIR_2], [INV_3], [DIR_3], [INV_4], [DIR_4], [INV_5], [DIR_5], [INV_6], [DIR_6]):
    
  2. Your indentation is messed up. Python is very particular about indentation, so be sure it's correct.

    This is incorrect:

     if not [INV_1] is None:
    myLabel += "\nINV {0}".format([INV_1]) & [DIR_1]
    

    As your myLabel += ... is part of the if block, you need to indent the second line further than the first line

     if not [INV_1] is None:
     myLabel += "\nINV {0}".format([INV_1]) & [DIR_1]
    

    Additionally, the line if not [INV_6] is None: is not indented far enough - it should line up with all the other if lines.

  3. I'm unsure what you're trying to do with the & in "\nINV {0}".format([INV_1]) & [DIR_1]. As far as I know, the ampersand in python is not used with text (see Stack Overflow Q&A What does & mean in python).
    It appears that you are trying to concatenate the two field values together - if so, put the [DIR_1] into your string format as well - something like this

    if not [INV_1] is None:
     myLabel += "\nINV {0}{1}".format([INV_1], [DIR_1])
    
  4. While not an issue that would cause an error, this is tidier and easier to read. Instead of if not [INV_1] is None:, use if [INV_1]:. Basically you're saying "if INV_1 isn't false" which is the same as saying "if INV_1 is true"

    if [INV_1]:
     myLabel += "\nINV {0}{1}".format([INV_1], [DIR_1])
    

I think the following update to your script should work better

def FindLabel ([TF], [INV_1], [DIR_1], [INV_2], [DIR_2], [INV_3], [DIR_3], [INV_4], [DIR_4], [INV_5], [DIR_5], [INV_6], [DIR_6]):
 myLabel = "TF {0}".format([TF])
 if [INV_1]:
 myLabel += "\nINV {0}{1}".format([INV_1], [DIR_1])
 if [INV_2]:
 myLabel += "\nINV {0}{1}".format([INV_2], [DIR_2])
 if [INV_3]:
 myLabel += "\nINV {0}{1}".format([INV_3], [DIR_3])
 if [INV_4]:
 myLabel += "\nINV {0}{1}".format([INV_4], [DIR_4])
 if [INV_5]:
 myLabel += "\nINV {0}{1}".format([INV_5], [DIR_5])
 if [INV_6]:
 myLabel += "\nINV {0}{1}".format([INV_6], [DIR_6])
 return myLabel
answered Jun 3, 2017 at 15:58
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.