0

I have a bunch of functions these functions append the string value 'FAIL or PASS in a list and depending on fail or pass a GUI window(PyQT5) should open up. But even though all the values in all the Lists are 'PASS' the window for 'FAIL' window opens up. I think my if else condition is wrong if someone could correct it for me or show me how to do it that would be much appreciated.

Please show me a more accurate or the correct way to do this.

def checkbox_ledNA(self):
        self.LedResult = []
        self.LED_pass_na = 'PASS'
        self.LED_fail_na = 'FAIL'
        pass_fail_na = lambda state_na: self.LED_pass_na if state_na == 2 else self.LED_fail_na
        self.checkPWR_rot_na = pass_fail_na(self.ui.PWR_rot_NA.checkState())
        self.LedResult.append(self.checkPWR_rot_na)
        self.checkPWR_grun_na = pass_fail_na(self.ui.PWR_grun_NA.checkState())
        self.LedResult.append(self.checkPWR_grun_na)
        self.checkP1_na = pass_fail_na(self.ui.Port1_NA.checkState()) 
def parameter_vergleich(self):
        self.confResult = []

        ####################################################
        if self.ord_nmr == self.artikel_na:
            print("Artikel number matched")
            self.ord = 'PASS'
        else:
            print("Artikel did not  match")
            self.ord = 'FAIL'
        self.confResult.append(self.ord)
        ####################################################
        if self.firmware_version == self.firmwareV_na:
            print("Firmware version matched")
            self.fv = 'PASS'
        else:
            print("Firmware version did not match")
            self.fv = 'FAIL'
        self.confResult.append(self.fv)
def get_output_rx(self):
        while True:
            self.size = self.ser.inWaiting()           
            if self.size > 190:
                self.rx = 'PASS'
                print(self.size, ' RX-Leitung ', self.rx)
                if self.rx == 'PASS':
                    break
            else:
                self.rx = 'FAIL'
                if self.rx == 'FAIL':   
                    break
                print(' RX-Leitung ', self.rx)
                break
            self.rxtxResult.append(self.rx)
            return self.rx

def checkResult_na(self):    
        if 'FAIL' in self.pingTestResult or self.rxtxResult or self.LedResult or self.confResult:
            self.failWindow()
            
        else:
            self.passWindow()
Asram
  • 3
  • 2

1 Answers1

1

The problem lies in your conditionals. Python evaluates a series of conditionals separated by or by evaluating each expression between successive instances of or and returning True or False for each conditional. So, writing

if 'FAIL' in list1 or list2 or list3

Is the same as saying

if 'FAIL' in list1 or if list2 or if list3:

which is syntactically wrong but conveys the meaning of what you're asking Python to do.

When presented with something like if list2, unless list2 is empty, the if statement will return True. So your statement:

if 'FAIL' in list1 or list2 or list3

evaluates to

if False or True or True

which evaluates to

if True 

making your 'FAIL' window open even when the actual 'FAIL' condition isn't satisfied.

If you want to check if the string 'FAIL' is in multiple lists, you need to describe each condition explicitly:

'FAIL' in list1 or 'FAIL' in list2 or 'FAIL' in list3

And so on.

If you have many strings to search and wanted a more compact way to do this you could place all the lists you want to search into a single list and then use any, like so:

list_of_lists = [list1, list2, list3, list4...]
search_string = 'FAIL'

if any([search_string in each_list for each_list in list_of_lists]):
    do_something()

Does this help?

Vin
  • 929
  • 1
  • 7
  • 14