Python 错误“TypeError: unorderable types: list() <= int()"; [英] Python Error &quot;TypeError: unorderable types: list() &lt;= int()&quot;

查看:36
本文介绍了Python 错误“TypeError: unorderable types: list() <= int()";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个程序,但我似乎反复遇到错误

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

类型错误:无法排序的类型:list() <= int()

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

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

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! ")

推荐答案

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

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:

这篇关于Python 错误“TypeError: unorderable types: list() <= int()";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆