[GIS] ArcMap field calculator error 000989 indention error

arcgis-desktoperror-000989field-calculatorpython-parser

I'm trying to make a four character output based on concatenating a single character field (in1) with either 1 or 2 zeros, depending on the length of in2, and then concatenating that . I'm getting an indention error on this but I can't see where. I looked at the other posts with this issue and none of their solutions are working for me.

def fixRouteName( in1, in2, out ):
  if len(in1) == 2:
    out = in1 + "0" + in2
  elif len(in2)  < 2:
    out =  in1 + "00" + in2
  else:
    out = in1 + in2
  return out 

__esri_field_calculator_splitter__

fixRouteName( !Exist_Partition! , !Exist_Route! , !Route! )

Here is the error message:

Description The code block used by the Calculate Field or Calculate
Value tool has a syntax error. This error message provided will list
the specific Python syntax error.

Solution This error code covers a number of Python syntax errors, for
example, Parsing error: unindent does not match any outer indentation
level (line 3).

The above is a Python syntax error. Line 3 has invalid indentation.
For specific Python issues, consult the Python documentation for more
information, or consult the Calculate Field, Calculate Value, or
Analyze Tools For Pro help for more information on these tools.

When returned by a script tool or when evaluating a script tool using
Analyze Tools For Pro, this error can also indicate that the tool's
source script cannot be found.

Best Answer

I tried your code on my machine (running ArcMap 10.3.1) and it worked, so I could not replicate your error. Try using an UpdateCursor in the ArcMap python interpreter (substitute 'your_shapefile' for the name of your shapefile):

import arcpy
with arcpy.da.UpdateCursor('your_shapefile', ['Exist_Partition', 'Exist_Route', 'Route']) as cursor:
    for row in cursor:
        if len(row[0]) == 2:
            row[2] = row[0] + "0" + row[1]
        elif len(row[1]) < 2:
            row[2] = row[0] + "00" + row[1]
        else:
            row[2] = row[1] + row[2]
        cursor.updateRow(row)

Also, ensure both the input fields ('Exist_Partition' and 'Exist Route') are text.

On a final note, in your original function you passed out as an input, but you don't need to. The function need only accept two arguments. This is the function that worked in field calculator on my machine:

def fixRouteName( in1, in2 ):
  if len(in1) == 2:
    out = in1 + "0" + in2
  elif len(in2) < 2:
    out = in1 + "00" + in2
  else:
    out = in1 + in2
  return out
Related Question