0

I am new to python and trying to learn some algorithm techniques.

can someone please help me understand what it is that I am doing wrong here? I am getting an error with

IndexError: list index out of range

this is my code

def binary_search(arr,item):
    #keeping track of elements in array
    low = 0
    high = len(arr)
    #mid section 
    while low <= high:
        mid = (low + high)
        guess = arr[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid - 1
        else:
            low = mid + 1
    return None

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

print(binary_search(a, 3))

Oscar Pacheco
  • 47
  • 1
  • 1
  • 7

0 Answers0