I'm attempting to create a tool that gives me the next available manhole number in a particular system. The manhole numbering structure looks like 1124-060E and therefore is a string field. Since inheriting, I've discontinued the letter extensions, because the sorting issues they cause. I've written a tool that has been working but is now spitting an error. The code is as follows:
def newMH(subbasin): lst =  with arcpy.da.SearchCursor(r"Sewer\Manholes",['OBJECTID','FACILITYID']) as cursor: for row in cursor: if subbasin.upper() in row.split('-'): if row.split('-').isdigit(): lst.append(row.split('-').zfill(6)) sorted(lst, key = lambda item: (item.isdigit(), item)) x = int(max(lst)) + 1 else: pass return str(subbasin).upper() +'-'+ str(x).zfill(4)
I'm saying if the 'subbasin' is equal to the first have in front of the dash then if the last half after the dash is a number then append it to my list. Then sort that list numerically then assign the max plus one to give me the next number in line then reconstruct the manhole format in my return line. The issue is the error I'm getting now.
"ERROR 000539: Error running expression: newMH('1124') Traceback (most recent call last): File "", line 1, in File "", line 13, in newMH UnboundLocalError: local variable 'x' referenced before assignment"