Kian L
Kian L

Reputation: 83

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()

Upvotes: 0

Views: 55

Answers (1)

FlyingTeller
FlyingTeller

Reputation: 20462

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:"))

Upvotes: 3

Related Questions