Search code examples
pythonpython-3.xlinear-search

My Linear Search Is not returning any index


I am writing a simple linear search program. But it does not return any index that I search for even though I have specified it to print the index when the user searches for an item in the list:

list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
tv = input("Search index:")

def LinearSearch():
    for i in range(0,len(list)):
        if list[i] == tv:
            print("Found at index ", i)

LinearSearch()

Solution

  • tv = input("Search index:")
    

    results in tv being a string, so comparison with an int will not work. You will need to convert tv to an int:

    tv = int(input("Search index:"))