Trying to replicate the code from this guide: Calculate Field Python examples—Data Management toolbox | Documentation
if (WellYield >= 0 and WellYield <= 10):
elif (WellYield > 10 and WellYield <= 20):
elif (WellYield > 20 and WellYield <= 30):
return 3 elif (WellYield > 30):
if (Id >= 0 and Id <= 10):
elif (Id > 10 and Id <= 20):
elif (Id > 20 and Id <= 30):
elif (Id > 30 and Id <= 40):
elif (Id > 40 and Id <= 50):
elif (Id > 50 and Id <= 60):
elif (Id > 60 and Id <= 70):
elif (Id > 70 and Id <= 80):
elif (Id > 80 and Id <= 90):
elif (Id > 90 and Id <= 100):
This is the error
ERROR 000539: Traceback (most recent call last):
File "<expression>", line 1, in <module>
File "<string>", line 2, in Reclass
NameError: name 'Id' is not defined
Failed to execute (CalculateField).
I don't know what Name Id is not defined means as i defined it just like the example.
You need to replace Level by Id without the ! marks. or... replace all the Id 's in your code block by Level.
If isn't a good idea to use the field name inside the def, but you did
This is something that I had originally tried, the Level there was just the most recent thing I had tried and when I ran out of things to try I came here. When I try either change you suggest I get a "parameter missing error" in red text at the bottom of the window.
Why isn't it a good idea to use a field name and what would I use alternatively?
Here is a picture of the window if that helps.
You are headed in the right direction here. COuld you send a screenshot of the error details? Since the code validated successfully, I suspect that the 'Id' field data type is not a numerical type (short/long integer or double) since the code is trying to write such values there.
The structure of your if/elif lends itself quite naturally to Python bisect — Array bisection algorithm — Python 3.8.2 documentation.
import bisect def Reclass(Id): return bisect.bisect_left(range(10,100,10),Id) + 1
The issue in the first screenshot is that you are not passing the field correctly. If the field in the GDB is called, Field1, then you pass it in the expression box as, !Field1! .The Id variable that is defined by the function is only used within the function. The variable name for the parameter in the function and the field in the GDB don't have to match.
My original code has a typo, I should have capitalized I in id on the usage in bisect function. I have updated the code. In your case, if the parameter name is "Level", then you need to use "Level" in the code and not "level". Python is case sensitive.
also check that the field containing the dat is indeed Id and doesn't have any trailing space.
And if you have any nulls in the field, you will have to edit your code OR, just query the field for all values no equal to None eg <null>