1

I am trying to write a program that finds happy numbers: more info here. This is not a school assignment, but I'm just trying to practice my Python 2.7. Basically, recursion is a vital part of this because I have to keep squaring every digit. In my code, I use recursion to keep running it, I have a base case, and it works, but for some reason, even though I call the function, no recursion occurs. It just returns the digits squares for the first number, which is a bug test, and it returns None, then it stops running. What's the problem?

num = raw_input('Pick a positive integer that you want to check for a happy number')
def happyn(number,second,third,fourth):
  if len(number) == 2:
    go = int(number[0])**2 + int(number[1])**2
  elif len(number) == 3:
    go = int(number[0])**2 + int(number[1])**2 + int(number[2])**2
  elif len(number) == 4:
    go = int(number[0])**2 + int(number[1])**2 + int(number[2])**2 +    int(number[2]**2)

  if len(number) == 1 and int(number) == 1 or int(number) == 7:
    return True
  elif len(number) == 1:
    return False
  else:
    print go
    go = str(go)
    go1 = go[0]
    print go1
    if len(go) == 1:
      go1 = go[0]
      happyn(go1,0,0,0)
    elif len(go) == 2:
      go1 = go[0]
      go2 = go[1]
      happyn(go1,go2,0,0)
    elif len(go) == 3:
      go1 = go[0]
      go2 = go[1]
      go3 = go[2]
      happyn(go1,go2,go3,0)
    elif len(go) == 4:
      go1 = go[0]
      go2 = go[1]
      go3 = go[2]
      go4 = go[4]
      happyn(go1,go2,go3,go4)


print happyn(num,0,0,0)
herpderp12346
  • 11
  • 1
  • 4
  • `go = int(number[0])**2 + int(number[1])**2 + int(number[2])**2 + int(number[2]**2)` looks like a bug. use `go = sum(int(x)**2 for x in number)` to replace all those cases – John La Rooy May 19 '17 at 04:05

2 Answers2

9

All the possible execution branches must return something, otherwise the recursion won't work. For example, this is wrong:

happyn(go1,0,0,0)

If nothing is explicitly returned, the function will return None. Therefore, the correct way is:

return happyn(go1,0,0,0)

The same goes for all the other exit points in your function.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1
  1. You are not returning the result of your recursive calls, ie return happyn(go1, 0, 0, 0). In Python, any function that ends without a return statement implicitly returns None.

  2. This is not really a good place to use recursion; a simple while loop would suit better.

    def sum_sq(i):
        total = 0
        while i:
            total += (i % 10) ** 2
            i //= 10
        return total
    
    def is_happy(i):
        while i >= 10:
            i = sum_sq(i)
        return i in (1, 7)
    
    def main():
        i = int(raw_input("Please enter a positive integer: "))
        if is_happy(i):
            print("{} is a happy number.".format(i))
        else:
            print("{} is an unhappy number.".format(i))
    
    if __name__=="__main__":
        main()
    
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99