1

I wrote this following several examples but it is still wrong. I need to populate a new field based on 5 ranges of values.

    def marks(Wards for try.JHRatio ): 
  if (Wards for try.JHRatio <= 50): 
     return 0
  elif Wards for try.JHRatio >50 and Wards for try.JHRatio <= 90):
     return 1
  elif (Wards for try.JHRatio >90 and  Wards for try.JHRatio <= 110):
     return 2
  elif (Wards for try.JHRatio >= 110 and  Wards for try.JHRatio <= 500):
     return 1
  elif (Wards for try.JHRatio > 500):
     return 0


    marks(!Wards for Try.JHRatio!)
5
  • You are missing a ( in the first elif.
    – MaryBeth
    Commented Jan 5, 2016 at 20:06
  • It would be helpful to see what your error message is in order to help solve
    – MaryBeth
    Commented Jan 5, 2016 at 20:07
  • Thank you! I added the (, but it still doesn't work. Commented Jan 5, 2016 at 20:12
  • The message says that 'Failed to execute. Parameters are not valid. Error 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 1). Failed to execute (CalculateField).' Commented Jan 5, 2016 at 20:14
  • Where did you find your examples? The try nested within your if statements doesn't seem right.
    – MaryBeth
    Commented Jan 5, 2016 at 20:21

1 Answer 1

1

you should use a simple variable name in your function : spaces will cause syntax errors (by the way, I recommend avoiding spaces in file names). it also makes your code more readable. Note that you do not need to do the ">" tests in your case, because the if statement already handle it.

   def marks(myfield): 
      if myfield <= 50: 
         return 0
      elif  myfield <= 90:
         return 1
      elif myfield <= 110:
         return 2
      elif myfield <= 500:
         return 1
      else:
         return 0


    marks(!Wards for Try.JHRatio!)
1
  • please do not ask questions inside comments. Make sure that the question hasn't been answered before, then ask a new question.
    – radouxju
    Commented Jan 6, 2016 at 6:12

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.