I am trying to give polygons values in a layer based on the sum population within other adjacent polygons. The polygons are adjacent as I have already sorted them through the appropriate field. This is my code:
def CreatePolls(DB_ID, pop):
for i in DB_ID:
SumPop = 0
PollCount = 1
SumPop += pop
if SumPop < 500:
return PollCount
elif SumPop > 500:
return PollCount
PollCount += 1
SumPop = 0
arcpy.CalculateField_management('DB_Test', 'Poll_Div', CreatePolls("!DB_ID!","!Population!"))
When I try to run it I get the following errors:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 5, in CreatePolls
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
Any ideas?
SumPop += "!Population!"
. You can't actually callCreatePolls
from within your python code. You need to write the code block as a string and pass that intoCalculateField
, as seen in the examples: pro.arcgis.com/en/pro-app/2.8/tool-reference/data-management/…return
at the same indent level will never be executed).