I am using ArcGIS Desktop 10.6.1. I create a new field of type short integer in the Attribute Table. My new field is named CIPOT and I want to populate it with values from the field CI, but only if values in field Z = 100, otherwise I want CIPOT to equal zero.

My Python script:

def NumValue( CI ):

  if (Z == 100):
    return CI
  else:  return 0

NumValue(!CI! )

Here is a screen shot:

enter image description here

I am getting an error message that global name 'Z' in not defined.

  • 6
    You need to pass in a variable into your function for Z. def NumValue(CI, Z): `NumValue(!CI!, !Z!)
    – Nathan W
    Feb 18 '19 at 23:16
  • 1
    !ci! If !z!==100 else 0 will work without block.
    – FelixIP
    Feb 19 '19 at 19:00
  • felixIP: that doesn't work. I get a syntax error.
    – TobyG
    Feb 20 '19 at 22:44

As @NathanW has commented, you have passed the value of your CI field to your function using defNumValue(CI):, but have not passed the value of the Z field.

You can pass the Z value by just adding it to your function variables

def NumValue(CI, Z):
    if (Z == 100):
        return CI
    else:  return 0


NumValue(!CI!, !Z!)

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.