AnsweredAssumed Answered

including Python variable in codeblock

Question asked by desertsp on Sep 22, 2016
Latest reply on Oct 7, 2016 by curtvprice

Can anyone explain what I've done wrong here. Have a feeling this is really simple...(new to Python). This code si being pasted into the Immediate window in ArcMap 10.1, in order to calculate a field with an "incrementing" value. I would like to specify the starting value externally.

 

Thanks!

 

This works:

arcpy.CalculateField_management("xx4_ncard","as_drv_srt","autoIncrement()","PYTHON_9.3","rec=0\ndef autoIncrement():\n global rec\n pStart = 1 #adjust start value, if req'd \n pInterval = 1 #adjust interval value, if req'd\n if (rec == 0): \n  rec = pStart \n else: \n  rec = rec + pInterval \n return rec")

 

This doesn't:

start = 1

arcpy.CalculateField_management("xx4_ncard","as_drv_srt","autoIncrement()","PYTHON_9.3","rec=0\ndef autoIncrement():\n global rec\n pStart = " + start + " #adjust start value, if req'd \n pInterval = 1 #adjust interval value, if req'd\n if (rec == 0): \n  rec = pStart \n else: \n  rec = rec + pInterval \n return rec")

Outcomes