# Field calculator expression

I am trying to reclassify my vector layer but I seem to have a mistake in my Code Block. The following is an example of what i want;

Old values:

• 10000
• 20000, 163000
• 230000 t/m 270000, 30000, 290000

New values:

• 10000
• 20000
• 30000

The code I have in my Code block is the following;

    def Reclass( Indexvalue ):
if (Indexvalue == 10000):
return 10000
elif (Indexvalue == 20000) or (Indexvalue==163000 ):
return 20000
elif (Indexvalue>= 230000 and Indexvalue <= 270000) or (Indexvalue == 30000) or (Indexvalue == 290000):
return 30000
elif (Indexvalue>= 40000 and Indexvalue <= 44000 ):
return 40000
elif (Indexvalue>= 50000 and Indexvalue <= 73000 ):
return 50000
elif (Indexvalue>= 80000 and Indexvalue <= 97000 ):
return 60000
elif (Indexvalue>= 110000 and Indexvalue <= 115000) or (Indexvalue>= 130000 and Indexvalue <= 140000 ):
return 70000
elif (Indexvalue>= 150000 and Indexvalue <= 161000) or (Indexvalue ==220000) or (Indexvalue == 221000) or (Indexvalue>= 170000 and Indexvalue <= 194000 ):
return 80000
elif (Indexvalue>= 200000 and Indexvalue <= 202000) or (Indexvalue == 280000):
return 90000
elif (Indexvalue== 210000):
return 100000


I found this code in a similar question like I had, but it does not work with me. Both VB and Python expressions give a syntax error so I think that the def and the if functions are not properly used by me.

Can someone point me in the right direction?

• Your code seems to have indentation issues (the last 3 elif are not at the same level than the previous 3, which in turn are not at the same level than the first 3). Is your code exactly the same than what you posted? Aug 29, 2017 at 13:19
• Syntax error -- Equivalence is "==" not "=". Aug 29, 2017 at 13:19
• The if's are in the code all on the same level, My apologies for the mistake in the question. I replaced all the "=" with "==" and I still have a syntax error. Aug 29, 2017 at 13:28
• Your first line should read def Reclass (Index): (without the exclamation marks). You use !Index! when you call the function, not when you define it. Aug 29, 2017 at 13:40

the code block should not contain field name but only variable names.

change !Index! into Index in your code block, but call your function with the field name

define function in code block (any name for your variable). Furthermore, you are testing equality with a single = when you should use 2 ==. This should also be changed in your codeblock. Finally, Python is a case sensitive language, so you must use or instead of OR

def Reclass( Indexvalue ):
if (Indexvalue == 10000):
return 10000
elif (Indexvalue == 20000) or (Indexvalue == 163000 ):
return 20000
elif ((Indexvalue>= 230000) and (Indexvalue <= 270000)) or (Indexvalue == 30000) or (Indexvalue == 290000):
return 30000


Use function

Reclass(!Index!)


As a remark, it is easier to find your mistake if you just start typing the first lines and then add into complexity. If you correct all but one error, you will still get an error message.

• @macroZED thanks, I made a copy-paste and forgot to update the line Aug 29, 2017 at 13:56
• So I did all of the above and I get a different error; Aug 29, 2017 at 14:01
• Nevermind I discovered a Typo. Thank you it worked! Aug 29, 2017 at 14:02
• also check parentheses (see my edit on third line), I've tested it on my side and it runs. Aug 29, 2017 at 14:12
• The code now works fine untill the fifth return. The first 4 returns work perfect, but after that eveything is assigned to 50000. I don't know why. Aug 30, 2017 at 9:34