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

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 19, 2017 at 1:40
3
  • 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): Commented Jul 19, 2017 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). Commented Jul 19, 2017 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. Commented Jul 19, 2017 at 1:48

1 Answer 1

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!)
answered Jul 19, 2017 at 1:46
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.