-1
print('This is a binary search!')
list1 = [1,3,6,9,12,23,67,68,69,71,74,86,95,100]
find = int(input('Which item would you like to find?'))
found = 0
def half(value):
if value % 2 == 1:
value = (float(value)/2) + 0.5
return int(value)
else:
return int(value/2)
length = len(list1)
cal = half(length)
pal = 0
while found == 0:
fund = int(list1[int(cal)])
if int(fund) == find:
print(str(cal + 1) + ' is the reference!')
found = 1
if fund > find:
cal = half(cal)
if fund < find:
cal = half(cal) + cal
爲什麼這不適用於列表中的某些值,我試圖創建一個二進制搜索程序。這段代碼爲什麼會在第1次11號和13號之外工作?
什麼不行呢?顯示一個例子。 –
有錯誤 回溯(最近一次通話最後): 文件「C:\ Users \ User \ Downloads \ Binary Search.py」,第20行,在 fund = int(list1 [int(cal)]) IndexError:列表索引超出範圍 –
我看不懂你的想法。請編輯您的問題*全部*相關信息 –