python in field calculator - calculate range of an area

335
2
06-22-2020 06:27 PM
Greta
by
New Contributor III

HI 

I would like to know why my expression is wrong each time I added Elif statement has a Syntax error

range = Reclass(!BP_sqm!)

def Reclass (BP_sqm):
if (BP_sqm <=150):
return 150
elif (BP_sqm > 150 and BP_sqm <=300):
return 300
elif (BP_sqm > 300 and BP_sqm <=500):
return 500
elif (BP_sqm> 500 and BP_sqm <=1000):
return 1000

2 Replies
DanPatterson
MVP Esteemed Contributor
def Reclass(BP_sqm):
    """Provide some context"""
    if (BP_sqm <= 150):
        return 150
    elif (BP_sqm > 150 and BP_sqm <=300):
        return 300
    elif (BP_sqm > 300 and BP_sqm <=500):
        return 500
    elif (BP_sqm> 500 and BP_sqm <=1000):
        return 1000
    else:
        return -999

# ----- Demo

vals = [149, 299, 499, 999, 9999]

for i in vals:
    print("{} -- {}".format(i, Reclass(i)))
    
149 -- 150
299 -- 300
499 -- 500
999 -- 1000
9999 -- -999

... sort of retired...
JoshuaBixby
MVP Esteemed Contributor

Greta Rocio Salas Menendez‌, your code is very similar to the functioning example code that Dan Patterson‌ provided (ignoring the spacing issue from not formatting code), which makes me wonder whether you are selecting the Python Parser