vendredi 3 mars 2017

Python - Weird IF statement while using nested lists

I don't understand why the else condition is not passed. Can somebody explain why?

#Python 3.4    
list=[]
list_of_list=[]

list.append("Test")
list.append("This")
list_of_list.append(list)
list=[]

print(list_of_list)
print(type(list_of_list))
print(len(list_of_list))

for element in list_of_list:
 if(element[0]=="Test"):
     print("[OK]")
 if(element[0]=="NO"):
     print("[OK]Condition is not verified")
 else:
     print("[OK]Why the condition is verified??")#<--------------------------

OUTPUT:

[['Test', 'This']]
<class 'list'>
1
[OK]
[OK]Why the condition is verified??

EXPECTED OUTPUT:

[['Test', 'This']]
<class 'list'>
1
[OK]

How can I fix the code?

Aucun commentaire:

Enregistrer un commentaire