Home > Back-end >  Filling up a list, using input, and then using index to know position of value? python
Filling up a list, using input, and then using index to know position of value? python

Time:10-14

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"

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)


 

CodePudding user response:

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))
  • Related