2

I need help in writing the python script in the field calculator. I have three fields namely: CODE COMPLEX Map1

Map1 is an empty field that I want to populate. I have a number of conditions to cater with in a single Map1 field. e.g. If CODE is 1.1.7 and COMPLEX is 4 then the output for Map1 should be 1.3, and these conditions continue with different CODE and COMPLEX values. I have been trying to write it like this but it doesn't work:

Parser Python

Code Block

def Reclass( !CODE! , !COMPLEX! ):
    if ( !CODE! == 1.1.7 and !COMPLEX! == 4):
        return 1.3
    elif ( !CODE! == 3.2.0 and !COMPLEX! == 4):
        return 1.3    
    else:
        return 0

Map1 =

Reclass( !CODE! , !COMPLEX! )

Can anyone tell me where I am making a mistake? Or any other way of doing it?

  • The fields in the code block are just variables, no exclamation marks. it should be def Reclass(my_code,my_complex) or something like that. The calling of it in the field box is correct. – Michael Stimson Nov 11 '14 at 3:05
  • Thanks Michael for a quick reply. I've edited the code block with no exclamation marks now, but I'm still getting an error. I am new to programming and probably making a simple mistake somewhere :( that I don't know of: Now its like this: def Reclass( my_code,my_complex): if (my_code == 1.1.7 and my_complex == 4): return 1.3 elif (my_code == 3.2.0 and my_complex == 4): return 1.3 else: return 0 – Juwairia Nov 11 '14 at 3:13
  • 1
    It's always best to edit your question rather than trying to include code in comments - your formatting options there are far superior. This Meta answer may help with framing and interacting on your questions. – PolyGeo Nov 11 '14 at 3:21
  • PolyGeo's answer is correct, copy that into your code block and you should be fine. If you're still having problems edit/update your question with the current code block either as text or alt+PrtScn and paste the field calculator dialog. Please ensure that you've selected the python language radio button. – Michael Stimson Nov 11 '14 at 3:42
4

The problem is in your code block.

Instead of:

def Reclass( !CODE! , !COMPLEX! ):
    if ( !CODE! == 1.1.7 and !COMPLEX! == 4):
        return 1.3
    elif ( !CODE! == 3.2.0 and !COMPLEX! == 4):
        return 1.3    
    else:
        return 0

try:

def Reclass( CODE , COMPLEX ):
    if ( CODE == "1.1.7" and COMPLEX == 4):
        return 1.3
    elif ( CODE == "3.2.0" and COMPLEX == 4):
        return 1.3    
    else:
        return 0

The exclamation marks are needed in your expression because there they are indicating field names but in the code block CODE and COMPLEX are Python variables and could be called a and b if you wanted.

The other problem is that your CODE field appears to be of type text so I have added quotes around the tests of its values. I cannot tell if your Map1 and COMPLEX fields are text or numbers so I have not put them there - you may need to!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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