Within an iterator in Model Builder in ArcGIS I am trying to calculate a field value based on the inline variable substitution. In the picture I have renamed the tools with the corresponding inline variable substitutions. The Calculate Value tool is a precondition before the calculate field tool is run.
In the Calculate field tool I get a value from a table using arcpy.da.SearchCursor script. When I run it for the first time, with the first wildcard, the iteration works fine. When I use a second wildcard, I get the error:
"UnboundLocalError: local variable 'numbers_facilities' referenced before assignment"
This is the script which I use in the calculate field tool:
def amount_facilities(): my_username = "%username%" selected_facility = "%facility%" #getting facility "CBR" (as an example) from inline variable substitution. my_tablenr = "%tablenr%" #refers to table to get value. input_table = "C:/Users/" + my_username + "/Dropbox/Project_One/Data/Input_Data.gdb/facilities_count" + my_tablenr query = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(input_table, "Facility_name"), selected_facility) with arcpy.da.SearchCursor(input_table, ['Facility_Count'], query) as cursor: for row in cursor: numbers_facilities = row[0] return numbers_facilitiesAfter doing some research, I realize this is a basic python syntax issue that should be able to be resolved but how can I do it using python script with a calculate field tool within an iterator in model builder. Can anybody help me out here how I can solve this UnboundLocalError?
أكثر...

In the Calculate field tool I get a value from a table using arcpy.da.SearchCursor script. When I run it for the first time, with the first wildcard, the iteration works fine. When I use a second wildcard, I get the error:
"UnboundLocalError: local variable 'numbers_facilities' referenced before assignment"
This is the script which I use in the calculate field tool:
def amount_facilities(): my_username = "%username%" selected_facility = "%facility%" #getting facility "CBR" (as an example) from inline variable substitution. my_tablenr = "%tablenr%" #refers to table to get value. input_table = "C:/Users/" + my_username + "/Dropbox/Project_One/Data/Input_Data.gdb/facilities_count" + my_tablenr query = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(input_table, "Facility_name"), selected_facility) with arcpy.da.SearchCursor(input_table, ['Facility_Count'], query) as cursor: for row in cursor: numbers_facilities = row[0] return numbers_facilitiesAfter doing some research, I realize this is a basic python syntax issue that should be able to be resolved but how can I do it using python script with a calculate field tool within an iterator in model builder. Can anybody help me out here how I can solve this UnboundLocalError?
أكثر...