琐碎间距错误,python?

use*_*929 0 python binary

甚至问这个我感到很尴尬,但是因为某种原因python让我的错误给了我"l"

    def binary_search(l, targetValue):
          low = 0, high = len(array)
          while low <= high:
                mid = (high - low)/2
                if l[mid] == targetValue:
                     return "we found it!"
                elif l[mid] > targetValue:
                     low = mid - 1;
                else        l[mid] < targetValue: #this line seems to be the problem
                     high = mid + 1;
          print "search failure :( "
Run Code Online (Sandbox Code Playgroud)

iCo*_*dez 6

虽然你的间距是不正常的,但实际上这不是问题所在.

问题是由于您正在使用else表达式这一事实.相反,您需要使用elif:

elif l[mid] < targetValue:
Run Code Online (Sandbox Code Playgroud)

或者,甚至更好,完全摆脱表达式,因为你已经测试过,l[mid] == targetValue并且l[mid] > targetValue:

else:
Run Code Online (Sandbox Code Playgroud)

else意思是"为了别的,做到这一点".因此,它不评估也不支持表达.