def bin_search(list,item):
low=0
high=len(list)-1
while low<=high: #4
mid = round(((low + high) / 2)+0.1,0) #1
#mid=(low + high) / 2
guess=list[int(mid)]
if guess==item:
return int(mid)
if guess>item:
high=mid-1 #2
else:
low=mid+1 #3
return None
my_list=[1,3,5,7,9]
print(bin_search(my_list,7))
注意二分法的思想,实现中四处标记的原理,以及第四处加等号和不加等号的影响