Q:

Code unable to handle big loops

Setup: Starting with the number 32, the sum of squares is 13
because 3x3 + 2x2 = 13. The sum of squares for 13 is 10 because 1x1 + 3x3 = 10. The sum of squares
for 10 is 1, so 32 is a happy number.
sample input
The input will be a single positive integer. Several examples are given here.
32
4565
42
5555
Sample Output
The program must print a sentence indicating if the input number is happy or unhappy, then exit.
32 is a happy number
4565 is an unhappy number
42 is an unhappy number
5555 is a happy number
Actual problem: whenever i input huge numbers such as 5555. i get an error such as:
File "C:/Users/username/Desktop/bob.py", line 43, in sos
sos()

File "C:/Users/username/Desktop/bob.py", line 26, in sos
for x in range(repeat):
RuntimeError: maximum recursion depth exceeded in comparison

**code: **
ans = input()
y = 5000 #this is so that the data does not loop forever
repeat = len(ans)
def sos():
    global y
    global repeat1
    global ans
    for x in range(repeat):
        List = list(str(ans))
        repeat1 = len(List)
        repeat2 = len(List)
        ans1 = int(List[int(repeat2)-int(repeat1)]) * int(List[int(repeat2)-int(repeat1)])
        #print("ans1", ans1)
        ans2 = int(List[int(repeat2)-(int(repeat1)-1)]) *int(List[int(repeat2)-int(repeat1-1)])
#print("ans2", ans2)
    ans = ans1 + ans2
    y -= 1
    if x == 1:
        print(this, "is a happy number")
    else:
        if y <=0:
            print(this, "is not a happy number")
        else:
            sos()

sos()

Question: What can i do to prevent this error?
request: please try to edit my code instead of creating your own. it help me learn what i did wrong and fix it in the future if needed.

A:

A number is either a happy number if the recursive sum of squares ends up in 1, or is not if it recurses endlessly (see the wikipedia definition). An easy way to implement such a behavior is to track the intermediate sums you calculate on the way:
def is_happy(num):
    return is_happy_recursive(num, [])

def is_happy_recursive(num, seq):
    sum_squares = sum([int(d)**2 for d in str(num)])
    if sum_squares == 1:
        return True
    elif sum_squares in seq:
        return False
    else:
        seq.append(sum_squares)
        return is_happy_recursive(sum_squares, seq)

