1

I'm trying to perform a selection in an attribute table based on two columns. The intital step I'm intending to perform is to select fields between values, e.g. (a > 0 and a < 10) and from this selection then in the second step find fields in another column, e.g. (if b == 1, return "OK" elif b == 2, return "NOT OK"). I think I'm safe with the logic behind it, but the Python scripting seems to be my drawback. What Am I doing wrong since I get parsing error everytime I try to execute the code?

def Check ( a, b ):
 for (a > 0) and (a < 10):
  if b == 2:
   return "NO"
  elif b == 1:
   return "YES"
  elif b == 0:
   return "MAYBE" 
 for (a => 10) and (a < 15):
  if b == 2:
   return "NO"
  elif b == 1:
   return "YES"
  elif b == 0:
   return "MAYBE"
 for (a > 15):
  if b == 2:
   return "YES"
  elif b == 1:
   return "NO"
  elif b == 0:
   return "MAYBE"

Result (Check)

1
  • You can use any indent scheme you want, so long as it's four spaces (this has become a defacto standard in Python coding)
    – Vince
    Commented Sep 21, 2017 at 10:53

1 Answer 1

5

Your main problem is the use of "for" instead of "if" with the conditionals related to the first field.

def Check ( a, b ):
 if (a > 0) and (a < 10): #if you don't have negative values, it could be if a<10: . Also, note that you will not return any result for negative values with the present script.
  if b == 2:
   return "NO"
  elif b == 1:
   return "YES"
  elif b == 0:
   return "MAYBE" 
 elif (a => 10) and (a < 15): #elif a<15:
  if b == 2:
   return "NO"
  elif b == 1:
   return "YES"
  elif b == 0:
   return "MAYBE"
 elif (a > 15): #else:
  if b == 2:
   return "YES"
  elif b == 1:
   return "NO"
  elif b == 0:
   return "MAYBE"

Also, make sure that you run it with

Check(!fielda!,!fieldb!)

By the way, in your example the value of "a" doesn't influence the output, so this could be skipped.

1
  • Perfect! The use of "for" was the problem. I thought an "if" followed by another "if", looked wrong before I even tried my self, but I learnt something today. Thank you! I'm sure the script can be shortened a lot, but this is simple enough and understandable for a non-pyhton audience.
    – chrkes
    Commented Sep 21, 2017 at 8:47

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.