'builtin_function_or_method'对象不可订阅

use*_*929 3 python algorithm

def binary_search(li, targetValue):
    low, high = 0, len[li] #error on this line
    while low <= high:
        mid = (high - low)/2
        if li[mid] == targetValue:
             return "we found it!"
        elif li[mid] > targetValue:
             low = mid - 1;
        elif li[mid] < targetValue:
             high = mid + 1;
    print "search failure "
Run Code Online (Sandbox Code Playgroud)

刚刚发布了这个问题,但我的代码仍然不起作用?

jra*_*rez 8

您使用了错误的括号len(li)len[li]

请记住,当您尝试访问需要使用的功能时,function(args)如果您使用的[]话实际上正在访问类似列表的序列.your_list[index].len是一个内置函数,所以你需要()


Mar*_*ers 5

len 是内置函数,但您尝试将其用作序列:

len[li]
Run Code Online (Sandbox Code Playgroud)

调用该函数:

len(li)
Run Code Online (Sandbox Code Playgroud)

注意那里的形状变化,索引用方括号完成,调用用圆括号完成。