Yashwanth O
Yashwanth O

Reputation: 15

To check whether an entered value exists in a List

This is my simple code. When a User enters a value, it has to be compared with the list I have used and should return the output as required. But when I execute the code and enter a value that is a part of the list, it returns the print statement under the else construct. Please help me with your suggestions

mylist=[1,2,3]
print('Enter the Rank:')
x=input()
if x in mylist:
    print('You have passed')
else:
    print('Sorry you have failed')

Upvotes: 0

Views: 97

Answers (1)

Banana
Banana

Reputation: 2533

The items in mylist are ints. Input returns a string, so to compare them you need to convert one of them. Either

x = int(input())

or

if int(x) in mylist

Upvotes: 2

Related Questions