This is because your are using list1.index(i)
list.index()
only returns the first occurrence of the matching element only. So even when your loop is finding the second occurence of any number, this function will return the first occurence's index only.
Since you are printing the index of the searched element, you can use enumerate
for that:
>>> list1 = [4,2,7,5,12,54,21,64,12,2,32]
>>> x=int(input("Please enter a number to search for : "))
Please enter a number to search for : 2
>>>
>>> for idx, i in enumerate(list1):
... if x==i:
... print("We have found",x,"and it is located at index number",idx)
...
We have found 2 and it is located at index number 1
We have found 2 and it is located at index number 9
enumerate
iterates over your list1
, and returns a tuple
value: idx, i
in each iteration, where i
is the number from your list1
, and idx
is its index.