I have a rudimentary knowledge of python, so it is possible that my understanding correct, but for the life of me I cannot get my if/elif/else statement to work. I will post it here and then explain what it is trying to do, and what it is actually doing:
Expression:
LandFcv_Adjusted = AdjustFCV(!PptDesc!,!LandFcv!,!ImpFcv!)
Code Block:
def AdjustFCV(PropType,Land,Imp):
homestead = ("500","501","502")
if Land == "NULL" or Imp == "NULL":
return 0
elif PropType == "Vacant Land" and Imp in homestead and Land not in homestead:
return Land
elif Land in homestead or Imp in homestead:
return 0
else:
return Land
Basically what this code is supposed to do is that it is running through parcel data, and the order is this:
- If either the land value or improvement value is null, then the field (land adjusted) is given 0.
- Then, if Imp is in one of the 500 codes, but land is not AND the property type is vacant, the land value should be returned.
- Then, for the remaining land that was not coded above, if either Land or Imp is in a 500 code, the adjusted land value should be 0.
- Finally, anything that did not fall under the conditions above has its original land value returned.
Except what happens is that the second elif statement which completely overrides the first elif statement. So under the first elif statement, where a parcel may have had its original land value returned because it is a vacant land, the land value is not in a 500 code, but the improvement value is a 500 code, the second elif statement seems to start the condition over again and completely ignores that elif statement, so then I am left with a parcel that has a 0 value returned even though it meets the first elif condition.
If I remove the second elif statement, the first elif statement runs as it should. If I make all elifs into if statements, it runs like it should. If I try a nested if statement like below (this is what ChatGPT recommended) it runs as it should:
if Land == "NULL" or Imp == "NULL":
return 0
if Imp in homestead:
if PropType == "Vacant Land" and Land not in homestead:
return Land
else:
return 0
else:
return Land
but if I change that second if statement to an elif instead of an if, it again returns a 0 where the vacant land condition is actually true.
Like I said, I have a rudimentary knowledge of python, but my understanding is that an elif section is supposed to move and ignore any previous record that was affected by an earlier condition statement. Instead, elif is ignoring all previous condition statements. Am I wrong in my understanding? What am I not getting?