Select to view content in your preferred language

IF STATEMENT Calculate Field Help ArcPro

976
2
Jump to solution
11-12-2021 08:13 AM
ABM2021
New Contributor

Hello everyone. I am new to coding and need some help using the calculate field option.

I have one field (!ARA!) and another field (!Class!). When area is a certain size, I would like to change the attribution of !Class! to be either 25, 50, 100, or 200. 

Should the field name be !Class!?

Expression type : Python 3

What should the expression be?

Is this the correct code block?

def Reclass(!ARA!):
if !ARA! is <= 1000:
return 25
elif !ARA! >= 1000:
return 50
elif !ARA! >= 2000:
return 100
elif !ARA! >= 5000:
return 150

I keep getting syntax errors. I will appreciate any help! Thank you in advance.

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

The code won't work as expected. The If statement will be satisfied with the first condition that evaluated to true, so inValue = 5000 would return 50. You want the logic to be like this instead.

 

def Reclass(inValue):
  if inValue is <= 1000:
    return 25
  elif inValue < 2000:
    return 50
  elif inValue < 5000:
    return 100
  else:
    return 150

 

 

View solution in original post

2 Replies
JoeBorgione
MVP Emeritus

 

def Reclass(!ARA!):
     if !ARA! is <= 1000:
         return 25
     elif !ARA! >= 1000:
        return 50
     elif !ARA! >= 2000:
        return 100
     elif !ARA! >= 5000:
        return 150

 

We can't tell if you've properly indented your def() as well as your if/elif so here ^ is what it should look.  That said I think your error is passing !ARA!.  Instead of passing that directly, try this approach:

 

def Reclass(inValue):
    if inValue is <= 1000:
        return 25
    elif inValue >= 1000:
        return 50
    elif inValue >= 2000:
        return 100
    elif inValue >= 5000:
        return 150

 

In your field calculator, enter :

reclass(!ARA!)

in the upper window.

Is !ARA! numeric?  If not that will be an issue as well.

Your logic is a little bit flawed as well: if !ARA! == 1,000 what  value do you really want?

 

 

That should just about do it....
KenBuja
MVP Esteemed Contributor

The code won't work as expected. The If statement will be satisfied with the first condition that evaluated to true, so inValue = 5000 would return 50. You want the logic to be like this instead.

 

def Reclass(inValue):
  if inValue is <= 1000:
    return 25
  elif inValue < 2000:
    return 50
  elif inValue < 5000:
    return 100
  else:
    return 150