1

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 code. 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). Below are two 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."enter image description here enter image description here

  • 2
    What was the error? You also seem to be returning strings, not numbers. – lynxlynxlynx Dec 11 '14 at 20:12
2

In general your condition statement should go as:

if TEST1 == 'FAIL':
    return 0

Single or double quotes for checking fields with string values, and when returning to a numeric field no quotes needed.

2

If I'm reading your logic correctly, if either Test1 or Test2 = FAIL, you want to return 0, but if both = PASS you want to return 1. I'd do something like 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
    return val
0

You could use a dict structure ({key: value}) to convert keywords like PASS to the standard bool type (i.e., True and False). After this conversion to bool, it is conventional logic:

def Final(t1, t2):
    c = {'PASS': True, 'FAIL': False}
    t1 = c[t1]
    t2 = c[t2]
    if t1 and t2:
        return 1
    else:
        return 0

Note that if you have other test values like GOOD or Pass, you will see a KeyError, so you can extend the dict structure, if necessary, to other synonyms that are either True or False.

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.