【问题标题】:TypeError: unorderable types: int() > list()TypeError:不可排序的类型:int() > list()
【发布时间】:2015-11-08 09:47:50
【问题描述】:
# Import Packages
import random

# Global Variables
perf_num = 500
species = [20]
temp_num = 0
length = 0
s = 0

# Main Program
for num in range(100):
    r1 = int(random.random()*10)
    r2 = int(random.random()*10)
    species.append(r1)
    length = len(species)
    while s < length:
        print(s)
        if species[s-1] > species[s]:
            temp_num = species[s-1] - r1
            species[s-1] = temp_num
        else:
            temp_num = species[s] - r1
            species[s] = temp_num
        if s-1 < 5:
            species[s-1] = []
        s += 1

    print(species)

由于我刚开始从 youtube 学习 Python,请不要用非常复杂的编码语言进行解释。我尝试制作自己的程序并继续收到此错误。

【问题讨论】:

    标签: python-3.x typeerror


    【解决方案1】:

    在下面一行:

    species[s-1] = []
    

    您将一个空列表分配给一个数字列表,结果如下:

    20 [20, 2] 2
    

    那么当你尝试比较一个数字和一个列表时:

    if species[s-1] > species[s]:
    

    你会得到那个错误:

    TypeError: unorderable types: int() > list()
    

    【讨论】:

    • 其实错误发生在if species[s-1] &gt; species[s]:这一行。这只是在python 3中,在python 2中不会发生错误(所有类型都可以在python 2中进行比较),所以在减法运算时会得到不同的错误。
    • python2 中的错误是一个不同的TypeError: unsupported operand type(s) for -: 'list' and 'int'
    • @Random832 嗯...我回家后会检查一下。谢谢!
    • @Random832 Thx,我认为你是对的,但你知道如何解决这个错误或以不同的方式做我想做的事吗?请尽快回答。非常需要为学校项目完成这项工作
    • @alfasin 知道如何在不出现此错误的情况下仍然做我想做的事吗?
    猜你喜欢
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多