-2
def divSum(number):
  divisors = [1]
  for i in range(2, number):
    if (number % i)==0:
      divisors.append(i)
  return sum(divisors)
l=[]
def isFriendly(x):
  if x==1:
    return False
  elif x in l:
    return True
  else:
    l.append(x)
    #x=divSum(x)
    isFriendly(divSum(x))

z = isFriendly(20)
print(z)

However, if I replace "return" with "print", it prints correctly. I tried a sample function (instead of calling isFriendly) to just accept a number and return True if the input number is 1, else False and it returned the correct output.

Any suggestions appreciated.

  • 4
    The `else` is not returning anything. Guessing you meant `return isFriendly(divSum(x))`. – kabanus Jun 22 '20 at 05:56
  • 4
    Duplicate of [Recursive function returning none in Python](https://stackoverflow.com/questions/19215141/recursive-function-returning-none-in-python) – Guy Incognito Jun 22 '20 at 05:57
  • what you are trying to achieve? what is in the else branch and friendly mean other than ==1 can you explain more clear about your requirement. – Vignesh Jun 22 '20 at 06:08
  • The first suggestion worked. Thanks. – MandelBroccoli Jun 22 '20 at 14:31
  • "However, if I replace "return" with "print", it prints correctly." Please also see [What is the purpose of the return statement? How is it different from printing?](https://stackoverflow.com/questions/7129285/). – Karl Knechtel Aug 12 '22 at 23:29

1 Answers1

0

You are not returning anything for the last else loop. That's why it is returning None. However, your list , l, does get populated. try printing l instead of z after calling the function.

MusHusKat
  • 32
  • 2