1

For some reason I cannot get this code to work in the Field Calculator of ArcGIS Desktop 10.6.1. I'm trying to use the Python parser to populate the field "SHAP_CLAS" with multiple conditional statements based on "AREA_LA" and "AREA_PERI_SQR".

enter image description here

The code I've tried so far is:

def calc(AREA_LA, AREA_PERI_SQR):
   if (AREA_LA =< 100 and AREA_PERI_SQR =< 0.65):
       return 2
   elif (AREA_LA =< 100 and AREA_PERI_SQR > 0.65):
       return 1
   elif (100 < AREA_LA < 500 and AREA_PERI_SQR =< 0.45):
       return 2
   elif (100 < AREA_LA < 500 and AREA_PERI_SQR > 0.45):
       return 1
   else:
       return 3

...and...

calc(!SHAP_CLAS!)

...in the box below the code block.

2 Answers 2

1

You've not told us what the error message you're receiving is, but from looking at your code I can see you're not using the correct operator for 'Less Than or Equal To'.

You've used:=< When it should be: <=

Correcting that should hopefully fix your issue.

2
  • 1
    It will not work. Because she is calling a method with two attributes with only one provided (and the wrong one also). But you are right about the operator. Dec 11, 2019 at 8:59
  • 1
    You are right about the operator. With @JanDoležal's code and '<=' it works, thank you! Dec 11, 2019 at 10:42
0

You were almost there, also call field calculator on the field you want to change ("SHAP_CLAS" for you):

def calc(AREA_LA, AREA_PERI_SQR): ##also change =< to <=
   if (AREA_LA =< 100 and AREA_PERI_SQR =< 0.65): 
       return 2
   elif (AREA_LA =< 100 and AREA_PERI_SQR > 0.65):
       return 1
   elif (100 < AREA_LA < 500 and AREA_PERI_SQR =< 0.45):
       return 2
   elif (100 < AREA_LA < 500 and AREA_PERI_SQR > 0.45):
       return 1
   else:
       return 3

and the change is here: (you are using method with 2 field, but then, you are providing only one field to the method)

calc(!AREA_LA!, !AREA_PERI_SQR!)

Basically the code is:

def method(field1, field2, fieldX):
    '''do something'''
    return value

and call:

method(!field1!,!field2!,!fieldX!)

There are some examples half way through at this page: http://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.