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

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

我正在尝试开发程序,但是错误似乎让我反复受到欢迎

TypeError: unorderable types: 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! ")

最佳答案

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:

关于python - Python错误 “TypeError: unorderable types: list() <= int()”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34561397/

相关文章:

javascript - 如何防止setInterval函数在Javascript中执行两次?

python-3.x - 如何 "normalize"python 3 unicode 字符串

algorithm - 将字节作为字符串转换为实际的字节类型

python-3.x - 类型标识和转换,以便在python中捕获错误

c++ - 没有匹配函数调用构造函数问题(关于类基础语法的问题)

python - Dask 从 HDFS 分发 : Reading . csv

python - webapp2 - 如何反转模板中的 URL?

python - 从文件名/字符串中删除非数字

javascript - nodejs 中的语法 babel

python - 学校项目的网络抓取