Simplicity
Simplicity

Reputation: 48916

Why am I getting this error - searching

I have the following code based on Python School:

EDIT: fixed the indentation of "position" and "return found", and used "raw_input

def linearSearch(item,my_list):
    found = False
    position = 0
    while position < len(my_list) and not found:
        if my_list(position) == item:
            found = True
        position = position + 1
    return found

bag = ['book','pencil','pen','note book','sharpner','rubber']
item = raw_input('What item do you want to check for in the bag?')
itemFound = linearSearch(item,bag)
if itemFound:
    print('Yes, the item is in the bag')
else:
    print('Your item seems not to be in the bag')

When I ran the program, I got the following:

What item do you want to check for in the bag?pencil
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    item = input('What item do you want to check for in the bag?')
  File "<string>", line 1, in <module>
NameError: name 'pencil' is not defined

EDIT: Getting the following error after the edits, although tried to put the item name between quotes

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    itemFound = linearSearch(item,bag)
  File "test.py", line 5, in linearSearch
    if my_list(position) == item:
TypeError: 'list' object is not callable

Why am I getting this error?

Thanks.

Upvotes: 0

Views: 122

Answers (4)

Copperfield
Copperfield

Reputation: 8510

I guess this some kind of homework, otherwise there is no need to implement this function just do item in bag. Anyway about the function, there is no need to keep track of the index like that, use range or xrange for that, or having a variable found, just do return True when found and at the end of the function do return False

def linearSearch(item,my_list):
    for position in xrange(len(my_list)):
        if my_list[position] == item:
            return True
    return False

you can also use the fact that a list is iterable and do

def linearSearch(item,my_list):
    for elem in my_list:
        if elem == item:
            return True
    return False 

Upvotes: 0

Iron Fist
Iron Fist

Reputation: 10951

my_list is a list, index it not call it as a function, so it should be :

if my_list[position] == item:

Another thing, if you are looking for one particular item in my_list, than just return from linearSearch as soon as you found it, no need to keep iterating through the rest of my_list:

if my_list[position] == item:
    found = True
    return found

Upvotes: 2

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

Replace input with raw_input to get a string: input tells Python 2 to evaluate the input string.

That said, your code has more problems: for instance, you increment the position in the wrong place.

Upvotes: 1

skywalker
skywalker

Reputation: 1300

The problem is, that you are using python2 and this is python3 code. So it would be best for you to install python3 and this code should run OK. Or in python2 you could you the raw_input function instead of input.

Upvotes: 1

Related Questions