0

I want to extract an index of just one item from the list

def main():
mylist = [21, 5, 8, 52, 21, 87]
num = input("What number: ")
if num in mylist:
    print(mylist.index(int(num)))

I tried this but it doesn't output like it should, it prints nothing.

AMC
  • 2,642
  • 7
  • 13
  • 35
NikoRan
  • 41
  • 5
  • 1
    num (the string) won't be in the list, you know this because you convert it to an integer before trying to get its index – Sayse Mar 02 '20 at 15:43
  • `input` gives you strings; you need to convert them to ints first – Jussi Nurminen Mar 02 '20 at 15:43
  • 2
    Also... please indent properly – Adam Mar 02 '20 at 15:43
  • What do you mean it prints nothing? This should raise an exception... And it will make it easier if you cast to int from the start: `num = int(input(...))` – Tomerikoo Mar 02 '20 at 15:48
  • Yeah it says Process finished with exit code 0 ... – NikoRan Mar 02 '20 at 15:55
  • _I tried this but it doesn't output like it should, it prints nothing._ ..... _it says Process finished with exit code 0_ How is that possible? **That code leads to an exception.** – AMC Mar 02 '20 at 17:21
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – AMC Mar 02 '20 at 17:21

1 Answers1

0

You forgot to parse the input

if int(num) in mylist:

Now it'll work :)