-3

For some reason this python linear search will not work. Every time I run it and input a value that is in the list, it does not tell me that the item is in the list and just runs through. It does not give any syntax error so must be something logical. Any ideas?

def linearSearch():
numbers = [3, 5, 54, 6, 17, 8, 32, 65, 87, 54]
pointer = 0
print("What would you like to search for?")
searchTerm = input()
try:
    val = int(searchTerm)
except ValueError:
    print("Please enter an integer")
    linearSearch()
while pointer < len(numbers):
    if numbers[pointer] == searchTerm:
        print("Item has been found at " + pointer)
    else:
        pointer = pointer + 1
Thomas Read
  • 619
  • 2
  • 6
  • 14
  • What did you see when you ran it with a debugger? – Scott Hunter Jun 07 '17 at 13:22
  • 1
    Why don't you try using the index functionality in python ? numbers.index(val) ?? – Satyadev Jun 07 '17 at 13:23
  • 1
    Why not just use `numbers.index(int(val))`? – MSeifert Jun 07 '17 at 13:23
  • 1
    You create a variable `val`, but you never use it after its assignment. Is that intentional? Also, it's not a great idea to do input validation with recursion; see [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/953482) for more information. – Kevin Jun 07 '17 at 13:23

3 Answers3

1
if numbers[pointer] == searchTerm:

I think you meant to write:

if numbers[pointer] == val:

searchTerm is a string (assuming we're in Python 3.X), so it will never compare equal to any of the integers in the list.

Additional problems:

  • You can't concatenate a string and an integer, so "Item has been found at" + pointer will crash. Try using print("Item has been found at", pointer) instead.
  • Your loop prints "Item has been found at..." infinitely, because you don't increment pointer when you find the item. Try adding a break after the print.
  • If the user doesn't enter an integer, you call linearSearch() again. But the original call is still on the stack, and will continue to execute once the inner call completes. Then it will crash with UnboundLocalError: local variable 'val' referenced before assignment, because val was never successfully created in that context. Consider a non-recursive approach, for example:

 

def linearSearch():
    numbers = [3, 5, 54, 6, 17, 8, 32, 65, 87, 54]
    pointer = 0
    print("What would you like to search for?")
    while True:
        try:
            val = int(input())
            break
        except ValueError:
            print("Please enter an integer")
    while pointer < len(numbers):
        if numbers[pointer] == val:
            print("Item has been found at", pointer)
            break
        else:
            pointer = pointer + 1

linearSearch()
Kevin
  • 74,910
  • 12
  • 133
  • 166
1

You should use val which is an int and not searchTerm which is a string when you compare with numbers[pointer]. Plus, don't forget to break your loop, else it will run indefinitely!

if numbers[pointer] == val:
        print("Item has been found at " + str(pointer))
        break

edit Also added a str(pointer) because it will thrown an error in Python3+ when you try to add int with string.

Nuageux
  • 1,668
  • 1
  • 17
  • 29
0

Linear Search :

 // funtion which rturns true if item found inside list.
    def linearSearch(list, value):
            for i in range(len(list)):
                if i == value:
                     return True

// Call above function pass list of values and item to search

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
item = 10
print(linearSearch(list, item)) // item to search
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147