【发布时间】:2021-05-23 13:29:13
【问题描述】:
我正在尝试编写一个名为aliquot_chain 的函数,它的参数是起始值a 和迭代次数n,都是非负整数。它应该返回一个长度为n+1 的列表,以a 开头,其中列表中的每个数字(从第二个开始)是前一个数字的因子之和,不包括该数字本身。
我一直收到这条消息:
- 不支持的操作数类型:“NoneType”和“int”。
谁能解释这意味着什么以及如何修复我的代码? (我需要在aliquot_chain函数中使用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))
【问题讨论】:
标签: python list error-handling while-loop