# Calculate Field with Python

Question asked by aarons@student.ethz.ch on May 7, 2017
Latest reply on May 11, 2017 by xander_bakker

Hello everyone

I have a question concerning Python in ArcMap:

What I want to do: I want to write a "2" in the field "Hauptgesellschaft_Gew" if the field "Hauptgesellschaft" contains the number "1235" (both fields are in the table "Wald_Poly"). This is part of an exercise given to us in which we have to calculate a suitability map.

What I have tried in the Tool "Calculate Field":

Input Table: Wald_Poly

Field Name: Hauptgesellschaft_Gew

Expression: reclass(!Hauptgesellschaft!)

Expression Type (optional): PYTHON

Code Block (optional): inTable = "Wald_Poly"
fieldName = "Hauptgesellschaft_Gew"
expression = "reclass(!Hauptgesellschaft!)"
codeblock = """def reclass(Hauptgesellschaft):
if Hauptgesellschaft == '1235':
return '2'"""

The field formats are text/string for "Hauptgesellschaft" and short integer for "Hauptgesellschaft_Gew".

Unfortunately it does not work, the programm runs, but there are no numbers written into the field "Hauptgesellschaft_Gew". We never had an introduction to Python so my (not working) solution is based on different examples I found on the internet.

The reason for this rather complicated way of solving this problem is, that I have to do everything in the model builder and that there are more complicated codes following. So if any of you know, how I could integrate an OR (like in if Hauptgesellschaft == '1235' OR Hauptgesellschaft == '1236' OR Hauptgesellschaft == '1237' ) I would be glad too.

I would be really happy, if somebody could help me. I tried for hours, but I could not get it working.

P.S. This is the first time I post something on this board, I am sorry, if I did something wrong.