0

これを聞くのも恥ずかしいのですが、何らかの理由で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 :( "
4

2 に答える 2

6

あなたの間隔は異常ですが、実際にはここでは問題ではありません。

問題は、式で使用しているという事実によって引き起こされていますelse。代わりに、次を使用する必要がありますelif

elif l[mid] < targetValue:

l[mid] == targetValueまたは、さらに良いことに、 andについて既にテストしているので、式を完全に削除しますl[mid] > targetValue

else:

else「それ以外はこうしなさい」という意味です。したがって、式は評価もサポートもされません。

于 2013-11-14T21:58:18.377 に答える