The Student Room Group

Python coding

I made this program in Python to perform a linear search but it doesn't work when i run it, have i done something wrong?
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
my_list = ['10,25,31,15,85,69,75,21,19,6']
item = input('What number would you like to search for?')
itemFound = linearSearch(item,my_list)
if itemFound:
print ('I have found your number!')
else:
print ('Sorry, I could not locate your number')
Original post by Ateacher
my_list = ['10,25,31,15,85,69,75,21,19,6']

I can't find any other issues with your code without executing it but an array of integers should not be enclosed with apostrophes because that will turn it into an array with only one string object.
Reply 2
thanks
Original post by HoldThisL
I can't find any other issues with your code without executing it but an array of integers should not be enclosed with apostrophes because that will turn it into an array with only one string object.

Quick Reply

Latest

Trending

Trending