1

I am trying to use Field Calculator to use a multiple conditional based on a string in another field of the same attribute table.

enter image description here

I have written the code below, but without success.

Pre-Logic Script Code:

def hazardValue( !Group! )
    if !Group! = '1.1':
        return 1
    if !Group! = '1.2':
        return 2
    if !Group! = '1.5':
        return 4        
    if !Group! = '2.1':
        return 3
    else
        return 5

Hazard =

hazardValue( !Group! )

It is always returning an error:

There was a failure during processing, check the Geoprocessing Results window for details

  • 2
    the !Group! is out of scope for the code block. Take out the exclamation marks in the code block only and change the assignment equals (=) to a conditional equals (==) then it should work. You will also need a colon on the else: statement and at the start def hazardValue(Group): – Michael Stimson Jul 19 '17 at 1:42
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. Please always include error messages as text as images sometimes don't display well on some devices, and text is also searchable (to help others find similar messages). – Midavalo Jul 19 '17 at 1:48
  • 3
    Also your message says "check the Geoprocessing Results window" - did you do that? The full error message will be contained in there. – Midavalo Jul 19 '17 at 1:48
2

You're pretty close I think. In an if statement, you need double equals ==, and you are passing your !Group! in your expression into a new variable (myfield) in your function.

Pre-Logic Script Code:

def hazardValue(myfield):
    if myfield  == '1.1':
        return 1
    elif myfield  == '1.2':
        return 2
    elif myfield  == '1.5':
        return 4        
    elif myfield  == '2.1':
        return 3
    else:
        return 5

Hazard =

hazardValue(!Group!)

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.