# --------------------------------- # the solution # --------------------------------- def is_happy(n): numbers_seen = [] while True: if n == 1: return True if n in numbers_seen: return False numbers_seen.append(n) n = sum_of_digits_squared(n) # --------------------------------- # a helper function # --------------------------------- def sum_of_digits_squared(n): total = 0 while n > 0: one_digit = n % 10 total += (one_digit ** 2) n = n // 10 return total # --------------------------------- # two functions that use is_happy() # --------------------------------- def find_happiness(): while True: n_str = input('Number to test: ') if not n_str: break n = int( n_str ) print( is_happy(n) ) def print_happy(limit): for i in range(limit): n = i + 1 if is_happy(n): print( n, end=' ' ) print()