Hi I have the following
This will iterate 10 times to begin with..I would like a prompt to user Y for Yes .N for No after it has reached 10 and then return a prompt to the user to enter a new number in a while loop,similar to what I have below..(if not a number ,repeat until a number is entered). but with the intention to just enter a new number of iterations..
At this point if N is entered exit the program...Y will display and infinite loop hence the return -1 ...and so will "" ..(empty)
Thanks for any help.
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()
first_try = my_func(n=10)
while True:
try:
print(next(first_try))
except StopIteration:
print("End of Iteration")
break
while True:
my_object = my_func(n=get_valid_user_input())
while True:
try:
print(next(my_object))
except StopIteration:
print("End of Iteration")
break