def collatz(num): num_pan = num % 2 if num_pan == 0: num_finally = num // 2 print (num_finally) else: num_finally = 3 * num + 1 print (num_finally) return num_finally def user(): while True: num = input(">>:") try: num = int(num) except ValueError as e: print ("error {},please enter a number".format(e)) continue break while True: num = collatz(num) if num == 1: break user() # >>:aa # error invalid literal for int() with base 10: 'aa',please enter a number # >>:10 # 5 # 16 # 8 # 4 # 2 # 1