2016-05-08 56 views
-1

這段代碼似乎給我一個錯誤,我的參數我不知道我在這段代碼中做了什麼錯誤。當試圖使用python 2.7在tkinter中運行二進制搜索時,如果我不使用gui只是行代碼,那麼程序工作正常。這是在tkinter gui python中出現錯誤的二進制搜索2.7

http://www.pythonschool.net/data-structures-algorithms/binary-search/

的代碼,我根據我的程序錯誤是:在Tkinter的回調回溯(最近通話最後一個)例外:文件「C:\ Python27 \ LIB \ LIB-TK \ Tkinter的。 PY」,線路1536,在調用返回self.func(*參數)類型錯誤:的binarySearch()到底需要2個參數(0給出)

from Tkinter import * 
import csv 

with open('scoresRecord.csv', 'rb') as f: 

reader = csv.reader(f) 
your_list = list(reader) 

root = Tk() 
root.resizable(0,0) 
root.title("Score Board") 


def binarySearch(myitem,myList): 
found = False 
bottom = 0 
top = len(myList)-1 
while bottom<=top and not found: 
middle = (bottom+top)//2 
if myList[middle]== myitem: 
    found = True 
elif myList[middle] < myitem: 
    bottom = middle + 1 
else: 
    top = middle-1 

return found 




if __name__=="__main__": 
    Scorelist = [row[0] for row in your_list] 
    Dismissallist =[row[1] for row in your_list] # create a list of only the   dismissal 
    Venuelist =[row[2] for row in your_list] # create a list of only the venue 
    item = int(input(entScore.get())) 

    Scorelist = map(int, Scorelist) 
    rowPos = Scorelist.index(item) 
    isitFound = binarySearch(item,Scorelist) 


if isitFound: 
    Score= Scorelist[rowPos] 
    Dismissal= Dismissallist[rowPos] 
    Venue= Venuelist[rowPos] 
else: 
    Score= ("Not Found") 
    Dismissal= ("Not Found") 
    Venue= ("Not Found") 



numScoreLbl.configure(text=Score) 
numDismissal.configure(text=Dismissal) 
outVenue.configure(text=Venue) 


#==========GUI Section==================# 
+0

一個很好的開始就是修正你的縮進,以便它真正匹配你正在運行的代碼。縮進在Python中至關重要,而且您發佈的內容根本無法運行。 –

回答

0

首先,請確保您的列表是有序的,否則結果可能是錯的。

其次,這樣做

Scorelist = list(map(int, Scorelist))#convert to list 
isitFound = binarySearch(item,Scorelist) 

儘管如此,如果你正在使用的.index方法,那麼你可以有更好的表現做這樣的事情:

try: 
    index = Scorelist.index(item) 
    isitFound = True 
except ValueError: 
    isitFound = False 

它採用了二進制搜索部分,但是你反正使用.index。所以這樣更好。希望它有助於:)

+0

Thanx男人你bindaas @ankitbindal – user6298707

+0

標記我的答案正確,如果它解決了你的問題 –