lundi 23 août 2021

Why is this statement executed if it belongs to the 'else' clause? [duplicate]

Hello everybody I am writing a simple Python program involving PyQt5. The task is to add an item from list widget A to list widget B only if the chosen one is NOT present on B.

So here is the code

def addItem(sel):
if ui1.listSessione.count() == 0: ui1.listSessione.addItem(sel)
else:
    for n in range(ui1.listSessione.count()):
        print('::' + ui1.listSessione.item(n).text())
        print(sel)
        if ui1.listSessione.item(n).text() == sel:
            print('same')
        else:
            print('not the same')
            # ui1.listSessione.addItem(sel)

In this way the logic works (obviously the item in not added since the last line is commented). If I uncomment the last line, that statement is ALWAYS executed, regardless of being inside the 'if' or inside the 'else'.

How can it be possible??

Aucun commentaire:

Enregistrer un commentaire