0

I want to take all positive values in a field and multiply them by -1000. To do this, I put together the following code block:

def myCalc(x):
  if (x > 0):
    x*(-1000)
    return x
  else:
    return x

and then I call the expression:

myCalc( !FieldName! )

However, when I execute this field calculation, I get no warnings or errors, but the values in my field also don't change. The numbers look like this (before and after the calculation):

enter image description here

What is wrong with my code?

2

You need to reassign x to your calculation:

def myCalc(x):
    if (x > 0):
        x = x*(-1000)
        return x
    else:
        return x

If you'd like to save some typing you can simplify your logic a little:

def myCalc(x):
    if (x > 0):
        x = x*(-1000)
    return x
  • Thanks! It's been a while since I had to do logic like this. It's always the little things that I miss. :D – Stella Sep 18 '17 at 18:49

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.