无法将列表与numb进行比较

2024-05-13 08:28:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图让我的代码在每次检测到一个系数较大的数字时更新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

Tags: oftheintegers代码inif数字types
1条回答
网友
1楼 · 发布于 2024-05-13 08:28:21

无法比较不同的对象类型。你知道吗

如果要比较长度,请使用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

相关问题 更多 >