jeudi 24 août 2017

or condition seems to fail in Python3 [duplicate]

This question already has an answer here:

I have a very long list like this:

old_list = ['cat','dog','bat','cat',...]

I am using the following code to copy this entire list into another with changes for some specific conditions:

new_list = []
for item in old_list:
    elif item == ("cat" or "bat"):
        new_list.append("a")
    elif item == "dog" or "fog" or "sog" or "log":
        new_list.append("o")
    else:
        new_list.append(item)

The problem is that in some cases "cat" and "bat" are correctly replaced by "a" but in some cases "cat" or "bat" remains as it is when I print new_list

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire