Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

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.

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

4 Answers 4

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.

share|improve this answer

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
share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.