【发布时间】:2018-08-04 23:41:41
【问题描述】:
对于作业,我必须进行二进制搜索,我按照步骤操作,但出现以下错误:
Traceback(最近一次调用最后一次):文件“X:/Computer Science/Python/February 2018/25 02 18/binary search.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.")
【问题讨论】: