I'm trying to convert some VB Calculate Field code to Python for use in ArcGIS Pro. This code would work in VB, but I'm unable to get the Python syntax to execute in all scenarios. I have a simplified example of what I trying to do below:
Field name = type
DetermineType(!lanes!,!speed!) - this goes in the expression box
The following is in the code block:
def DetermineType(Lanes, Speed):
if Lanes.rstrip() == "1" and int(Speed.rstrip()) < 30:
return 1
elif Lanes.rstrip() == "2" and int(Speed.rstrip()) < 30:
return 2
elif Lanes.rstrip() == "3" and int(Speed.rstrip()) > 30:
return 3
else:
return 4
This will work as long as a row of data has values for both the Lanes and Speed field attributes. However, if one of these fields is null, I would expect a value of 4 to be returned to populate the type field but instead, my code block doesn't get executed at all. I end up with null in the type field.
My question is this: How do I populate a field if either of the input fields used to determine its value is null?
By the way, I've tried several experiments, including just concatenating !lanes! and !speed! in the Expression box, but as long as at least one of those fields is null, I get a null result.