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

你离开我真会死。 提交于 2019-12-13 03:24:40

问题


I'm trying to develop a program, however I seem to be repeatedly greeted by the error

TypeError: unorderable types: list() <= int()

This happens when I'm executing 2 if loops within each other. To give a bit of backstory into the problem, I am trying to make my program determine which difficulty a user has selected, and based on this, cause the program to measure a different amount of words within a file that the user has selected prior to this point.

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:


WordCount is a global variable. You assign it to the result of the split() which is a list which you then compare to a number 10 later. Essentially, you're comparing an int with a list. You should be careful on your variable names.. I would be confused by your naming conventions since there's several variables named similarly.

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


def GameStage03_E():
    if WordCount <= 10:


来源:https://stackoverflow.com/questions/34561397/python-error-typeerror-unorderable-types-list-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!