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.

  • Why are you returning the function definition in return def FindLabel ...? You would return the label. Also, make sure the indentation is correct - from the code you pasted it looks wrong. – Alex Tereshenkov May 24 '17 at 20:42
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
  • Thank you Midavalo, I believe my indentation is correct in the actual code block in ArcMap, transferring it did not work as intended. I appreciated your help. I always have difficulty the few times a code, and just need to start reading some books on python. Thank you again – G.Daigle Jun 9 '17 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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