简体   繁体   中英

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')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM