这个python提示我的错误我感觉我写的是对的,为什么会报错呢?
程序如下:
# 定义函数 binary_search 接受参数 list_sort, size, targer,并将查找结果返回
def binary_search(list_sort, size, targer):
low = 0, high = size
def dipole(low, high):
if (low + high) % 2 == 0:
return int((low+high)/2)
else:
return int(((low+high)-1)/2)
while low <= high:
mid = dipole(low, high)
if list_sort[mid] < targer:
high = mid - 1
elif list_sort[mid] > targer:
low = mid + 1
else:
return mid
try:
print(binary_search([1,2,3,4,5,6,7,8,9], 9, 6))
except:
print(-1)
报错的图片如下
有没有人解释一下