Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filling up a list, using input, and then using index to know position of value? python

I am trying to understand how to use index when i got a list from input.

count = 0
myListe = []

while True:
  
  
  empty_list = input("Input 5 values here: ")  
  myListe.append(empty_list)
  count += 1
  
  if count == 5:
    break  

Now i want to get an input that ask, what value in the list do you want to know the index number for.
So if i have ["New York", "Oslo", "Tokyo", 4, 3]

I want to be able to write for example Tokyo and then get back, "Its position 2"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have been googling and checkin w3school but i an not sure how to do this

I got this code that i copied from somewhere but it is not working for me.

index = input("What value do you want to know the position for?")
index = int(index)
index = myList[index]
print(index)


 

>Solution :

If I’m understanding correctly, you are looking for something like this:

arr = ["New York", "Oslo", "Tokyo", 4, 3]
arr = [str(el) for el in arr]

x = input()

if x not in arr:
    print(f"{x} is not included")
else:
    print(arr.index(x))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading