I'm trying to create a binary search function, I've attempted it with the code below but am a beginner at python ; I'm getting the error "list indices must be integers, not str" on the line i == alist[i]
. Could you please help me fix the problem? Here's the whole code:
def bsort(alist, i):
left = 0
right = len(alist)-1
i == alist[i]
[mid] = (left + right)//2
if alist[mid] < i :
left = [mid] + 1
elif alis[mid] > i :
right = [mid] + 1
elif alist[mid] == i :
print ("word found at", i )
elif left > right:
print ("Not Found!")
i = input("Enter a search >")
alist = ["rat","cat","bat","sat","spat"]
bsort(alist, i)
See Question&Answers more detail:os