为什么这个定义不起作用?

时间:2016-01-19 17:45:59

标签: arrays search binary function

def BinarySearch(searchItem,searchList):
    lower=0
    upper=len(searchList)-1
    while True:
        if lower== upper:
            return False,-1
        middle=(lower+upper)//2
        middleItem=searchList[middle]
        if searchItem==middleItem:
            return True,middle
        elif searchItem<middleItem:
            upper=middle
        else:
            lower=middle+1

BinarySearch(4,[0,1,4,7,8,9])

当我运行程序时,没有输出如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是因为你没有收到函数的返回并打印它

des, val = BinarySearch(4,[0,1,4,7,8,9])

if(des == True):
    print "item present at", val
else:
    print "item not present"