def my_func(c=1, n=-1): while True: if n>-1 and c > n: # if counter > number of iterations required break a = int(c * (c + 1)/2) yield a c += 1 def get_valid_user_input(): user_input = input("Enter a number, or leave blank for infinite loop: ") if user_input == "": return -1 try: return int(user_input) except ValueError: print("Error! Please enter a number or leave blank.") return get_valid_user_input() my_object = my_func(n=get_valid_user_input()) while True: try: print(next(my_object)) except StopIteration: print("End of Iteration") break
my_object = my_func(n=10)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)