【问题标题】:Why isnt this definition working?为什么这个定义不起作用?
【发布时间】:2016-04-25 07:51:31
【问题描述】:
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])

当我运行程序时没有输出如何解决?

【问题讨论】:

    标签: arrays search binary function


    【解决方案1】:

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

    des, val = BinarySearch(4,[0,1,4,7,8,9])
    
    if(des == True):
        print "item present at", val
    else:
        print "item not present"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2012-03-06
      • 2010-10-26
      • 2017-06-12
      • 2017-06-07
      相关资源
      最近更新 更多