Ateacher
Badges: 2
Rep:
?
#1
Report Thread starter 2 years ago
#1
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')
0
reply
HoldThisL
Badges: 21
Rep:
?
#2
Report 2 years ago
#2
(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.
1
reply
Ateacher
Badges: 2
Rep:
?
#3
Report Thread starter 2 years ago
#3
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.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

How are you feeling about starting university this autumn?

Really excited (62)
22.22%
Excited but a bit nervous (126)
45.16%
Not bothered either way (36)
12.9%
I'm really nervous (55)
19.71%

Watched Threads

View All