I've been self learning Python. Now I want to play a little with the Python code block so I can be more comfortable building my own expressions later on. Right now I am just playing with this water resource layer. I created the new field called "play", basically what I want to do is write a simple expression so that if the "WETLAND_TY" field holds the value of "Freshwater Forested/Shrub Wetland". the new "play" field will return a value of 1, otherwise it will return value 5.

Below is the screenshot of my current window The current window

I don't know what goes wrong here.

I know this issue can be dealt simply with select by attribute. But I am learning to build my expressions for some more complicated projects, so I'd like to finish it with Python expression.

In the process of playing, I made the "play' field like this: enter image description here and changed my code block to:

def NewValue(Area):
'''I was playing with a different field: Area'''        
if Area >= 0 and Area < 30000:
    return 1
elif Area >= 30000 and Area < 800000:
    return 2
    return 3

And this time it worked. I just don't understand what's wrong with what I wrote the first time.

Could anyone help me with this?

1 Answer 1


In the first instance, you list MyChange as the value for Play. The code doesn't recognize this as a function definition, so it's looking for a variable called MyChange in the codeblock, but there's no MyChange variable outside of the if statement. (I assume -- it might ONLY accept function inputs from the code block based on the error message displayed).

In the second instance, you've created a function via def functionname(parameters): which has return values. Play = NewValue(!Shape_Area!) calls the function, passing the value of the Shape_Area field for whatever row it's on in the Field Calculation.

To fix your Wetland Type, just modify it so that you have the codeblock style from the second example process:

def NewValue(wettype):
    if wettype == "Freshwater Forested/Shrub Wetland":      
        return 1
        return 5

or you could write it as such:

def NewValue(wettype):
    if wettype == "Freshwater Forested/Shrub Wetland":      
        MyChange = 1
        MyChange = 5
    return MyChange

Either way you would now call the function as Play = NewValue(!WETLAND_TY!).

When the variable is within the codeblock, it's just a variable - no need to include the exclamation marks around the variable/field name. When you're inserting the value of a field in Field Calculator/arcpy, you need to include the exclamation marks.

  • Thank you so much, both for answering the question and pointing out the mistake I had. I am very new to Python and am just playing with it to gain more experience. So I take it that I have to define a function in the code block to make it work? Simple statements in my first instance wouldn't work?
    – Bowen Liu
    Commented May 22, 2018 at 19:18
  • Yeah, now that I look at the error message more closely the Field Calculator is explicitly looking for a function definition within the code block.
    – SMiller
    Commented May 22, 2018 at 19:20
  • Ok, gotcha. Thanks a lot. The reason I'm doing this is because I'm trying to rewrite my colleague's VBScript Code in Python. I can somewhat read the code although I only know a bit Python. His code doesn't seem to have definition of a function. It begins with "Dim Earlier Year, LaterYear, MyChange." Then at the end of each if statement, it shows "MyChange = 2/3/4/5". So I thought that I don't need to actually define a function, rather just assign a value to a random variable. So I guess since ArcGIS Pro only uses Python, I have to play ball? Or did he define a function in VB already?
    – Bowen Liu
    Commented May 22, 2018 at 20:41
  • I also can read VBScript but not necessarily write it. Python's pretty useful overall so I recommend learning it. If you really want to know the difference in how Python vs. VBScript works in ArcPy search or post a separate question (due to focused nature of site) or check out Esri documentation. Or this Potentially useful article about converting VB to Python
    – SMiller
    Commented May 22, 2018 at 20:51
  • I know this is late reply. But I really appreciate your help. Still learning here.
    – Bowen Liu
    Commented Aug 3, 2018 at 13:05

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.