无法将列表与数字进行比较

时间:2016-11-02 23:13:49

标签: python python-3.x

我试图让我的代码在每次检测到具有更多因子的数字时更新mostComp的值,但仍然遇到以下情况:

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

我目前的代码如下:

def MostComposite(integers):
    #The item in sequence 'integers' of positive integers which has
    #the greatest number of distinct factors, returns None if the sequence is empty.
    mostComp = 0
    for i in integers:
        if (Factors(i)) > 0:
            mostComp += i
            if (Factors(i)) > mostComp:
                mostComp += i
    return mostComp

1 个答案:

答案 0 :(得分:0)

无法比较不同的对象类型。

如果要比较长度,请使用len(Factors(i))。 您的代码应如下所示:

def MostComposite(integers):
    #The item in sequence 'integers' of positive integers which has
    #the greatest number of distinct factors, returns None if the sequence is empty.
    mostComp = 0
    for i in integers:
        if len(Factors(i)) > 0:
            mostComp += i
            if len(Factors(i)) > mostComp:
                mostComp += i
    return mostComp