我刚刚发布了一个关于此代码的问题,我很抱歉再次这样做,但是我的return语句不起作用。每当我尝试运行代码时,它都会要求一个全局变量的位置,我试图在搜索方法中返回。任何帮助表示赞赏。谢谢。不返回声明
def main():
names = ['Ava Fischer', 'Bob White', 'Chris Rich', 'Danielle Porter','Gordon Pike', 'Hannah Beauregard', 'Matt Hoyle', 'Ross Harrison', 'Sasha Ricci', 'Xavier Adams']
binarySearch(names, "Ava Fischer")
print("That name is at position "+str(position))
def binarySearch(array, searchedValue):
begin = 0
end = len(array) - 1
position = -1
found = False
while not found and begin<=end:
middle=(begin+end)//2
if array[middle] == searchedValue:
found=True
position = middle
elif array[middle] >searchedValue:
end = middle-1
else:
first = middle+1
return position