繁体   English   中英

TypeError: 不支持的操作数类型 -: 'NoneType' 和 'int'

[英]TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

我正在尝试编写一个名为aliquot_chain的 function ,它的参数是起始值a和迭代次数n ,都是非负整数。 它应该返回一个长度为n+1的列表,以a开头,其中列表中的每个数字(从第二个开始)是前一个数字的因子之和,不包括该数字本身。

我一直收到这条消息:

- 不支持的操作数类型:“NoneType”和“int”。

谁能解释这意味着什么以及如何修复我的代码? (我需要在aliquot_chain function 中使用while True 。)

def factor_sum(a):
    s=0
    if a==0:
        return s
    else:
        if a%(a**0.5)==0:
            k=int((a)**0.5)
            s+=k
        else:
            k=int((a)**0.5)+1
        for n in range(1,k):
            if a%n==0:
                s +=n
                s +=int(a/n)
def aliquot_chain(a):
    l=[a]
    while True:
        l.append(factor_sum(l[-1])-l[-1])
        if len(set(l))!=len(l):
            del l[-1]
            return l
        
print(aliquot_chain(28))

如果if a==0计算结果为 false,Factor_sum 不会返回任何内容,因此它返回 None,这就是您尝试执行 None-something 的原因

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM