Hello,
I'm trying to process a Python If statement using Field Calculator. What I want is to calculate the value of a string field by evaluating the values recorded in a double field. Essentially, calculating travel mode based on speed ranges (0 = stationary, 1-6 = walk, 7-19 = cycling, 20+ = driving).
However, my expression fails to process in ArcPro. Below is the expression in full, along with the output I received:
Solved! Go to Solution.
First is you are getting a lock acquisition error, do you have multiple copies open of the table you are attempting to update?
Second use something like TravelMode = speedType (!Speed!) . "Type" is a function in Python and should not be used as a def name. It will work, but is poor practice.
Third for the Code Block, the way you have it coded "sp" is not assigned a value you have assigned the speed to "modey" when you called your function. "sp" is only defined as a global and never set. Your logic also does not handle NULL values or negative values that could be in your data.
def speedType(sp):
if sp == 0:
return "stationary"
elif 1 <= sp <=6:
return "walk"
elif 7 <= sp <= 19:
return "cycle"
else:
return "drive"
First is you are getting a lock acquisition error, do you have multiple copies open of the table you are attempting to update?
Second use something like TravelMode = speedType (!Speed!) . "Type" is a function in Python and should not be used as a def name. It will work, but is poor practice.
Third for the Code Block, the way you have it coded "sp" is not assigned a value you have assigned the speed to "modey" when you called your function. "sp" is only defined as a global and never set. Your logic also does not handle NULL values or negative values that could be in your data.
def speedType(sp):
if sp == 0:
return "stationary"
elif 1 <= sp <=6:
return "walk"
elif 7 <= sp <= 19:
return "cycle"
else:
return "drive"
check your conditions … if sp = 5 it should be in the range 1-6
sp = 5.
1 < sp <= 6 # ---- good
True
1 < sp >= 6 # ---- not correct
False
Thanks for that catch. I was looking at the code and not the logic in my responce. Adjusted my reply as well...