[GIS] Using defined function code block with Python in ArcGIS

arcgis-desktopfield-calculatorpython

I am running a Field Calculation within a Python script in ArcGIS. Within this Field Calculation I am calling a defined function within a code block.

Here is the dialogue that I ran and works:
enter image description here

This worked. However, I cannot get this to translate into my Python code:
enter image description here

Here's the code:

#Calculate YSLB
expression = "output(!MTH_BRNT!,!YR_BRNT!,!YSLB!)"
code_block = "def output(MTH_BRNT,YR_BRNT,YSLB):/n month = str(month_val)/n month_current = int(month)/n month_fire = int(MTH_BRNT)/n year = str(high_year)/n year_current = int(year)/n year_fire = int(YR_BRNT)/n Mc = (12*(year_current-1)) + month_current/n Mf = (12*(year_fire-1)) + month_fire/n return (Mc-Mf)/12"
arcpy.CalculateField_management(fuel_age_out,"YSLB",expression,"PYTHON_9.3",code_block)

Best Answer

I have met this problem before. Try to use """ instead of " above the expression.

Otherwise, try this one :

code_block = """def output(MTH_BRNT,YR_BRNT,YSLB):
 month = str(month_val)
 month_current = int(month)/n month_fire = int(MTH_BRNT)
 year = str(high_year)/n year_current = int(year)
 year_fire = int(YR_BRNT)
 Mc = (12*(year_current-1)) + month_current
 Mf = (12*(year_fire-1)) + month_fire
 return (Mc-Mf)/12"""

sometimes python doen"t rescognize the \n