0

I have been doing some Leetcode recently to prepare for interviews and I have recently come across a problem with my code. I was wondering what was wrong with this code and why my code will not return false when it is supposed to. Any help is appreciated.

def isHappy(n):
    num = [int(i) for i in str(n)]
    newSum = 0
    for i in num:
        newSum += i ** 2
    if newSum != 1:
        print('repeating')
        try:
            isHappy(newSum)
        except RecursionError:
            return False
    return True

print(isHappy(2))
Zach Frank
  • 23
  • 6
  • 1
    You're returning False at the very deepest level of recursion, but you're not doing anything to propagate the value up to the higher levels. The function should do something to check the result of the next deepest level of recursion, e.g. with `return isHappy(newSum)`. – entrez Mar 08 '22 at 02:56
  • 1
    Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none) – Stef Mar 08 '22 at 09:27

0 Answers0