[英]TypeError: unsupported operand type(s) for *: 'NoneType' and '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.