python - Error while returning list -
this code finding sum of factors of number. list returned in find factors function guess not accessible sum function because when printing list giving right list.
the error receive : typeerror: object of type 'nonetype' has no len()
def perfect_number(n): def sum(list,counter,value): if counter==len(list): print (value) else: sum(list,counter+1,value+list[counter]) def find_factors(n,a,list): if (a==n): print (list,len(list)) homecoming (list) elif (n%a )== 0: list.append(a) find_factors(n,a+1,list) elif (n%a) !=0: find_factors(n,a+1,list) xyz = find_factors(n,1,[]) homecoming sum(xyz,0,0) perfect_number(6)
you're missing homecoming statements:
def is_perfect_number(n): def sum_recursive(factors,counter,value): if counter==len(factors): homecoming value else: homecoming sum_recursive(factors, counter+1, value+factors[counter]) def find_factors(n,a,factors): if (a==n): homecoming elif (n%a) == 0: factors.append(a) find_factors(n,a+1,factors) elif (n%a) !=0: find_factors(n,a+1,factors) xyz = [] find_factors(n, 1, xyz) homecoming sum_recursive(xyz,0,0)==n print is_perfect_number(6) print is_perfect_number(28) print is_perfect_number(228)
python
No comments:
Post a Comment