2015-11-02 63 views
0

我正在嘗試編寫二進制搜索並且無法使其工作。該數組是預先確定的,程序正在搜索的值需要是輸入。我到目前爲止是:TypeError試圖在python中執行二進制搜索時

MYVALUE = input 
ARRAY = ["3", "5", "2", "9", "1"] 
MAX = len(ARRAY) 
MIDPOINT = int(MAX/2) 
FOUND = False 
while (FOUND == False): 
    MIDPOINT = int(MAX/2) 
    if MYVALUE > MIDPOINT: 
     MIN = int(MIDPOINT) 
     MAX = len(ARRAY) 
    elif int(MYVALUE) < int(MIDPOINT): 
     MAX = int(MIDPOINT) 
     MIN = 1 
    elif int(MYVALUE) == int(MIDPOINT): 
     print("Value found. Value is " + int(MIDPOINT)) 
    else: 
     print("Value not found") 

這不斷返回以下錯誤,我敢肯定還有其他問題。

Traceback (most recent call last): 
    File "/Users/auroraguild/Desktop/binary search.py", line 8, in <module> 
    if MYVALUE > MIDPOINT: 
TypeError: unorderable types: builtin_function_or_method() > int() 

謝謝!!!

+1

myvalue的輸入=() – mingaleg

+0

和你BINSEARCH看起來非常髒,有一個很好的例子:HTTPS://hg.python .org/cpython/file/3.5/Lib/bisect.py – mingaleg

+0

您不能在該列表上執行二進制搜索,因爲它沒有按順序。 – kindall

回答

0

你想:

MYVALUE = input() 

不:

MYVALUE = input