Jan-19-2018, 08:55 PM
The first issue with your code is that you compare a string against an integer. The input() is always a string so you have to convert it to an integer.
Checking for an element in a list is simple:
Checking for an element in a list is simple:
if element in list
In [1]: listA = [1, 3, 99, 58, 22, 77, 8, 2] In [2]: x = int(input('Give me a number: ')) Give me a number: 14 In [3]: if x in listA: ...: print(listA.index(x)) ...: else: ...: print(-1) ...: -1 In [4]: x = int(input('Give me a number: ')) # here the string returned from the input is converted to an integer Give me a number: 22 In [5]: if x in listA: # pure English :D I love Python! ...: print(listA.index(x)) # get the index of the first x in the list ...: else: ...: print(-1) ...: 4