I am trying to use Field Calculator to use a multiple conditional based on a string in another field of the same attribute table.
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
-
2the !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– Michael Stimson2017年07月19日 01:42:44 +00:00Commented 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).Midavalo– Midavalo ♦2017年07月19日 01:48:20 +00:00Commented Jul 19, 2017 at 1:48
-
3Also your message says "check the Geoprocessing Results window" - did you do that? The full error message will be contained in there.Midavalo– Midavalo ♦2017年07月19日 01:48:50 +00:00Commented Jul 19, 2017 at 1:48
1 Answer 1
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!)
Explore related questions
See similar questions with these tags.