mercredi 8 mars 2017

python call function inside for loop and function contain if else structure

I have the following python code that call a function that iterate over a dict. the code below not work as expected:

list1=["test1","test2","test3","test4"] list2=["toto1","test1","test3","toto4"]

def functest(t): for l in list2: if t == l: return cond1 else: return "rien"

for t in list1: titi=functest(t) print titi

When I print titi var I have toto1 printed 4 times.

If I remove the else inside my function the code seem to work.

How you can explain this behavior ?

Why when I add else with a returned string only the string is printed.

thanks

Aucun commentaire:

Enregistrer un commentaire