Python 3.x Python二进制搜索(';非类型';对象不可下标) 作为ISP导入InsertionSortPackage inp=input(“输入一个数字列表:”).split(“,”) inp=[inp中j的浮点(j)] num=输入(“要查找的编号:”) a=ISP.插入(inp) n=len(inp) num=浮点(num) def binary_search(): 未找到=真 第一个=1 last=n #这一定和while循环有关 而(未找到): 如果第一次
它现在正在打印:“你想找到的号码:2 您的号码不存在。 您的号码不存在。Python 3.x Python二进制搜索(';非类型';对象不可下标) 作为ISP导入InsertionSortPackage inp=input(“输入一个数字列表:”).split(“,”) inp=[inp中j的浮点(j)] num=输入(“要查找的编号:”) a=ISP.插入(inp) n=len(inp) num=浮点(num) def binary_search(): 未找到=真 第一个=1 last=n #这一定和while循环有关 而(未找到): 如果第一次,python-3.x,binary-search,Python 3.x,Binary Search,它现在正在打印:“你想找到的号码:2 您的号码不存在。 您的号码不存在。 在位置2“ISP.insertion(inp)找到的号码是内联插入。因此,结果不是一个列表。你必须使用inp,而不是a…有什么问题吗?既然a=ISP.insertion(inp)和insertion(…)不返回任何内容,a将是None,你不能像None[1]那样调用。。。 import InsertionSortPackage as ISP inp = input("Enter a list of number:").s
在位置2“
ISP.insertion(inp)
找到的号码是内联插入。因此,结果不是一个列表。你必须使用inp
,而不是a
…有什么问题吗?既然a=ISP.insertion(inp)
和insertion(…)
不返回任何内容,a
将是None
,你不能像None[1]那样调用。。。
import InsertionSortPackage as ISP
inp = input("Enter a list of number:").split(",")
inp = [float(j) for j in inp]
num = input("Number you want to find:")
a = ISP.insertion(inp)
n = len(inp)
num = float(num)
def binary_search():
not_found = True
first = 1
last = n
#Its gotta have something to do with the while loop
while(not_found):
if first < last:
print("Your number does not exist.")
mid = (first + last)//2
# Maybe you can make only print the last repeat for the while loop
if a[mid] == num:
print("Your number found at location " + str(mid+1))
not_found = False
else:
if a[mid] < num:
first = mid + 1
elif a[mid] > num:
last = mid - 1
binary_search()