I'm relatively new to python(3.3) and I'm just trying to do a binary search through a list of words, and cant figure out how to fix my operand types when it comes down to looping through the indices... I continue to get the TypeError. Cant figure out any way around it
def find(L, target):
start = 0
end = len(L) - 1
while start <= end:
middle = (start + end)// 2
midpoint = L[middle]
if midpoint > target:
end = midpoint - 1
elif midpoint < target:
start = midpoint + 1
else:
return midpoint
I'm calling the function as so:
L = ["Brian", "Meg", "Peter", "Joe", "Stewie", "Lois"]
find(L, "Joe")
midpointis a string. What shouldmidpoint - 1do?middle - 1,middle + 1.