【发布时间】:2017-03-03 10:15:48
【问题描述】:
我的二进制搜索函数代码不断收到此错误
ex = [3, 4, 19, 42, 53, 23, 5, 8, 20] #list used
number = input("What number do you want to find?: ")
length = len(ex) - 1
first = 0
last = [len(ex)]
done = False
while done == False:
for i in range(length):
if ex[i] > ex[i+1]:
sort = False
ex[i], ex[i+1] = ex[i+1], ex[i] #flips the numbers in the list
else:
print (ex)
mid = first + last / 2
found = [ex(mid)]
if number > found:
first == mid
if number < found:
last == mid
if number == found:
print ("number found")
print (str(found))
问题似乎是 mid = first + last / 2 等式...
【问题讨论】:
-
您已将
last定义为[len(ex)],所以last 确实是一个列表。 -
您在 else 语句中遇到了与
found类似的问题。 -
这不是您要寻找的答案,但请记住,布尔值不需要使用
==检查,而是可以使用while not done表示法来简化您的代码。