2016-05-08 57 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

标记我的答案正确,如果它解决了你的问题 –