对于功课,我必须进行二元搜索,我按照步骤操作,但是我收到以下错误:
回溯(最近一次呼叫最后一次):文件" X:/计算机 Science / Python / February 2018/25 02 18 / binary searching.py",第15行, 在 end = aList.index [middle] TypeError:' builtin_function_or_method'对象不可订阅
这是我的代码:
aList = [3,13,24,27,31,39,45,60,69]
end = len(aList) - 1
found = 0
start = 0
target = int(input("Please input the item you would like to search for:\t>"))
while start <= end and found == 0:
middle = int((start + end) / 2)
if aList[middle] == target:
found = 1
print(target , "is in the list.")
if target < aList[middle]:
end = aList.index[middle]
else:
start = aList.index[middle]
if found == 0:
print("The search item was not found.")
答案 0 :(得分:0)
在此处查看In Python, what does it mean if an object is subscriptable or not?
我这样解释:“ [要成为可下标对象,必须执行 getitem ()方法”
index[middle]
的意思是“从字典index
中提取具有等于变量middle
的键的值”
第15行应如下所示-函数调用:
end = aList.index(middle)