I am attempting to add a new field called "TimeYrCode" to a list of shape files, and then add values to this field based on the value of the "gridcode" attribute, which represents different days of the year from 1-365.
The code to calculate this field works in ModelBuilder. Exported to a python script, the code reads as follows
# ADD AND CALCULATE FIELDS TO LAYERS IN THE WORKSPACE
for fc in fcList1:
arcpy.AddField_management(fc, "TimeYrCode", "SHORT", "4", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(fc, "TimeYrCode", "myfunc( !gridcode!)", "PYTHON", "def myfunc(gridcode):\\n if (gridcode >= 60) and (gridcode< 152):\\n return 1\\n elif (gridcode >= 152) and (gridcode< 244):\\n return 2\\n elif (gridcode >= 244) and (gridcode< 335):\\n return 3\\n elif (gridcode >= 335) or (gridcode< 60):\\n return 4\\n else:\\n return 9")
When I attempt to run the code in PyCharm, I obtain the following error message
def myfunc(gridcode):\n if (gridcode >= 60) and (gridcode< 152):\n return 1\n elif (gridcode >= 152) and (gridcode< 244):\n return 2\n elif (gridcode >= 244) and (gridcode< 335):\n return 3\n elif (gridcode >= 335) or (gridcode< 60):\n return 4\n else:\n return 9
^
SyntaxError: unexpected character after line continuation character
Does anyone know what could be happening here? New to python and troubleshooting these kinds of issues