samedi 23 décembre 2017

Why is there a logical error with using the keyword 'and' in an if statement with dict?

I have a empty dictionary which I assign keys from a list of letters and values from user input

valuesDict = {}
letters = ['S', 'u', 'v', 'a', 't']
i = 0
while i < 5:
    newValue = input('Enter ' + letters[i] + ' ')
    if newValue != '':
        valuesDict.update({letters[i]: newValue})
    i = i + 1

and a cutdown if statement to show my problem which prints a number corresponding to the items in the dictionary

if 'S' and 'v' not in valuesDict.keys():
    print('1')
elif 'u' and 'v' not in valuesDict.keys():
    print('2')

If I enter values for u, a, and t it outputs '1' correctly

Enter S
Enter u 2
Enter v
Enter a 5
Enter t 7
1

However, when I enter values for S, a, and t for the elif part of the statement '1' is outputted when it is meant to be '2'

Enter S 2
Enter u 
Enter v 
Enter a 5
Enter t 7
1


Why is this happening, and how can I fix this and avoid it in the future?

Aucun commentaire:

Enregistrer un commentaire