Python错误“TypeError:unorderable types:list()< = int()”

时间:2016-01-02 00:16:16

标签: python python-3.x syntax-error typeerror

我正在尝试开发一个程序,但是我似乎一再受到错误的欢迎

  

TypeError:unorderable类型:list()< = int()

当我在彼此之间执行2 if个循环时会发生这种情况。为了给这个问题提供一些背景故事,我试图让我的程序确定用户选择了哪个难度,并基于此,使程序测量文件中不同数量的单词用户在此之前选择的内容。

Pastebin:http://pastebin.com/RZ5uKrfx

def WordCount(FileSelection):
    WrdCount = 0
    for line in ReadFile:
        Words = line.split()
        WrdCount = WrdCount + lens(Words)
    return WrdCount

def E_Mode():
    GameInitiationButton.config(state=NORMAL)
    global DifficultyState
    DifficultyState = "Easy"

def H_Mode():
    GameInitiationButton.config(state=NORMAL)
    global DifficultyState
    DifficultyState = "Hard"

def GameStage01():
    global GameStage01Button
    HardModeButton.destroy()
    EasyModeButton.destroy()
    GameInitiationButton.destroy()
    SelectTextLabel.destroy()
    SelectButton = Button(root, text='Select File', bg="grey1", fg="snow", font="consolas 9",
        command=GameStage02, height=1, width=30)
    SelectButton.place(relx=0.5, rely=0.7, anchor='c')
    GameStage01Button = Button(root, text='Initiate Game!', bg="grey1", fg="snow", font="consolas 9",
        command=GameStage_E_H, state=DISABLED, height=1, width=30)
    GameStage01Button.place(relx=0.5, rely=0.85, anchor='c')

def GameStage02():
    global ReadFile
    global WordCount
    FileSelection = filedialog.askopenfilename(filetypes=(("*.txt files", ".txt"), ("*.txt files", "")))
    SelectTextLabel.destroy()   

    with open(FileSelection, 'r') as file:
        for line in file:
            WordCount = line.split()
    print(WordCount)
    GameStage01Button.config(state=NORMAL)

    # GameStage03_E()

def GameStage_E_H():
    if DifficultyState == "Easy":
        GameStage03_E()
    elif DifficultyState == "Hard":
        GameStage03_H()

def GameStage03_E():
    if WordCount <= 10:
        tkinter.messagebox.showinfo("ERROR", " Insufficient Amount Of Words Within Your Text File! ")

1 个答案:

答案 0 :(得分:1)

WordCount是一个全局变量。您将它分配给split()的结果,该结果是一个列表,然后您可以将其与数字10进行比较。基本上,您将int与列表进行比较。你应该小心你的变量名。我会对你的命名约定感到困惑,因为有几个变量命名相似。

....
 with open(FileSelection, 'r') as file:
        for line in file:
            WordCount = line.split()
    print(WordCount)


def GameStage03_E():
    if WordCount <= 10: