I am not sure whether this question was asked in a similar way in the past, but I didn't find an answer specific enough for me to understand it.
I tried to check values of a dictionary within a for loop with an if statement that has multiple conditions.
First I tried this code which didn't work:
dict = {2312: 'Jim', 2323: 'Anna', 2314: 'Tim', 3054: 'Marc', 3574: 'Valerie', 3698: 'Nina'}
Smith = list()
Miller = list()
for name in dict.values():
if name == 'Jim' or 'Anna' or 'Tim':
Smith.append(name)
else:
Miller.append(name)
print(Smith)
print(Miller)
Then I tried this which worked:
dict = {2312: 'Jim', 2323: 'Anna', 2314: 'Tim', 3054: 'Marc', 3574: 'Valerie', 3698: 'Nina'}
Smith = list()
Miller = list()
for name in dict.values():
if name == 'Jim' or name == 'Anna' or name == 'Tim':
Smith.append(name)
else:
Miller.append(name)
print(Smith)
print(Miller)
My question is why does the first one not work but the second code does?
And is there a 'easier' code to check this?
Aucun commentaire:
Enregistrer un commentaire