0

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()
tobias_k
  • 81,265
  • 12
  • 120
  • 179
Kian L
  • 73
  • 8
  • Does it not _print_ or not _return_ a result? Also, is this Python 2 or Python 3? Because, depending on which it is, `tv` might be an `int` or `str`. – tobias_k Feb 08 '18 at 09:56
  • it does not "print" and it is in Python 3 – Kian L Feb 08 '18 at 10:02
  • Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – tripleee Jan 16 '19 at 08:38

1 Answers1

3
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:"))
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53