0

My code for binary search function in a list returns true for a value in the list, but returns None (instead of false) for values not in the list.

Can someone please explain me what I'm doing wrong?

The program is:

def searchlist(x,alist):
 end=int(len(alist)-1)
 mid=int(len(alist)/2)
 while len(alist)>2:
 if x==alist[mid] or x==alist[0] or x==alist[end] :
 return("true")
 break
 elif x>alist[mid]:
 alist=alist[mid:]
 mid=int(len(alist)/2)
 end=int(len(alist)-1)
 elif x<alist[mid]:
 alist=alist[:mid]
 mid=int(len(alist)/2)
 end=int(len(alist)-1)
 else:
 return("false")
aList=[2,3,5,7,9,12,14,23,34,45,67,89,101]
xnum=int(input("enter a number:"))
searchlist(xnum,aList)
print(searchlist(xnum,aList))
mkrieger1
24.2k7 gold badges69 silver badges85 bronze badges
asked Nov 19, 2018 at 12:59
3
  • Without diving into the code, alist is going down to 2 values and it's ending the while loop, causing it to return None. I'd suggest print out the current state of the list each loop, and manually run through your code to figure at what point it's not working as expected. Edit: Actually, it looks like your else statement won't ever execute. Put return False after the while loop instead and it should work (I'd suggest use True and False instead of strings). Commented Nov 19, 2018 at 13:09
  • If you want to return false if you don't find what you want in your list you should put the return false after the loop, not in the loop Commented Nov 19, 2018 at 13:12
  • your second elif perfomed. If you delete this block, then return ('false') Commented Nov 19, 2018 at 13:17

2 Answers 2

1

You get None when your function does not return a value. This happens because the while loop terminates without going into the "else" branch. A better practice would be to return True (not the string, but the Boolean value) when you find the value in the list, and return False after the loop.

answered Nov 19, 2018 at 13:14
Sign up to request clarification or add additional context in comments.

Comments

0

Your while loop cannot catch the else statement. you don't need that else. try this :

def searchlist(x,alist):
 end=int(len(alist)-1)
 mid=int(len(alist)/2)
 result = False
 while len(alist)>2:
 if x==alist[mid] or x==alist[0] or x==alist[end] :
 result = True
 elif x>alist[mid]:
 alist=alist[mid:]
 mid=int(len(alist)/2)
 end=int(len(alist)-1)
 elif x<alist[mid]:
 alist=alist[:mid]
 mid=int(len(alist)/2)
 end=int(len(alist)-1)
 return result
aList=[2,3,5,7,5,67,89,101]
xnum=int(input("enter a number:"))
print(searchlist(xnum,aList))
answered Nov 19, 2018 at 13:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.