0

I'm following the book about algorithm with python3. but my code isn't work. It looks definetely same as the book. Where is wrong?

def binary_search(list, item):
    low = 0
    high = len(list)-1

    while low <= high:
        mid = (low+high)//2
        guess = list[mid]

        if guess == item:
            return mid
        if guess > item:
            high = mid - 1
        else :
            low = mid + 1
    return None

my_list = [1, 3, 5, 7, 9]

print(binary_search(my_list, 3))
Emma
  • 1
  • 1
  • 1
    Also, Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. – cs95 Oct 31 '17 at 12:07
  • I think your code actually works! It returns the correct index of the item to be searched. – Miraj50 Oct 31 '17 at 12:09
  • @Miraj50 Thanks for your comment :) I will check again. I am not familiar with PyCharm. maybe that's a problem. – Emma Nov 01 '17 at 11:30
  • @cᴏʟᴅsᴘᴇᴇᴅ If I was looking rude, I am really sorry. Actually, I should have learned how to use PyCharm and asked something in the community. Those who have no knowledge of the subject may be difficult to ask questions. anyway, I am sure I make an effort of this problem. thanks for your comment :) – Emma Nov 01 '17 at 11:44

0 Answers0