Trouble Calculating a Field

3015
10
Jump to solution
12-11-2014 09:29 AM
MarkAccettulla
New Contributor

I don't know a whole lot about python and scripting, so I was hoping somebody could give me a couple pointers on fixing some python.  I am trying to calculate a "final results" attribute field.  The field type is numeric, because I want the values to either be a 1 (pass) or 0 (fail).  I have attached 2 variations of the code I have tried, but they both give me errors.  The two fields I am trying to calculate from are both text fields, and the values for both are either "PASS" or "FAIL."

I know that this should be a simple task, but like I said I am very very new to python.  Any help or pointers would be much appreciated.

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

If the fields are numeric then return 0 or 1 if they are text enclose them in double quotes.  Also Pass and Fail need to be in double quotes. Also take your def into a real python IDE and test it...your indentation could be off

Also since you have so few combinations check this logic using nested if.  No code def required...not tested but you can play with the logic

"0" if !Test1! == "Fail" else ("0" if !Test2! == "Fail" else "1")  # python of course

View solution in original post

10 Replies
KevinBell
Occasional Contributor III

Your 2.jpg is really close.  I think you need quotes around "FAIL" and "PASS", but not around 1 or 0.  You probably need to wrap your field names with ! as well.

0 Kudos
MarkAccettulla
New Contributor

Making those changes to my 2.jpg gives me an "invalid syntax" error that I wasn't getting before

0 Kudos
DanPatterson_Retired
MVP Emeritus

If the fields are numeric then return 0 or 1 if they are text enclose them in double quotes.  Also Pass and Fail need to be in double quotes. Also take your def into a real python IDE and test it...your indentation could be off

Also since you have so few combinations check this logic using nested if.  No code def required...not tested but you can play with the logic

"0" if !Test1! == "Fail" else ("0" if !Test2! == "Fail" else "1")  # python of course

MarkAccettulla
New Contributor

Dan, thank you.  I played with the logic and got it to work without code def.  I hadn't thought of doing it that way, because I am attempting to duplicate a workflow that had used code def

0 Kudos
curtvprice
MVP Esteemed Contributor
"0" if !Test1! == "Fail" else ("0" if !Test2! == "Fail" else "1")  # python of course

I would argue this isn't very Pythonesque. I would not shy away from a code block if it makes it easier to debug.

🙂

0 Kudos
DanPatterson_Retired
MVP Emeritus

their code block is unreadable...checking posts where proper indentation is often the cause has led me to use one-liners...we need a proper python mini-IDE for field calculations

curtvprice
MVP Esteemed Contributor

Couldn't agree more. If there is any complexity I try to remember to open an IDLE window to write the code and then paste it into that ugly little dialog box.

Maybe they should give us a button to open an IDE like with tbx script tool parameter validation code interface...

DanPatterson_Retired
MVP Emeritus

One can obfuscate further by combining list comprehensions with compound comparisons with reclass

>>> combined_is_best = ["Good" if x == "Curtis" else ("Best" if x == "Dan" else "Better") for x in ["Curtis","Dan","Curtis","You","get","it?"]]
>>> combined_is_best
['Good', 'Best', 'Good', 'Better', 'Better', 'Better']
>>>

Though you might like this one Curtis

Zeke
by
Regular Contributor III

If you want 0 if either Test1 or Test2 = 'FAIL', and 1 if both = 'PASS", try this:

def Final(t1, t2):
  if t1 == 'FAIL' or t2 == 'FAIL':
      val = 0
  elif t1 == 'PASS' and t2 == 'PASS':
      val = 1
  else:
      val = 2  # this allows you to check if t1 or t2 had some other value, (except for the first case, at least one = 'FAIL')
  return val