Edit: I should have said up front that I know this isn't the best way to do it; I was specifically trying to use recursion for this to learn about it.
I'm new to Python with beginner knowledge of programming and I'm running into an issue with recursion and error-handling. I have this function that takes user input to get a variable for my program:
1 def get_seconds():
2 a = input("Run how often in seconds (1-60, q to Quit)? ")
3 if a.lower() == "q":
4 exit()
5 try:
6 int_a = int(a)
7 if int_a < 1 or int_a > 60:
8 print("Interval must be between 1 and 60.")
9 get_seconds()
10 return(int_a)
11 else:
12 return(int_a)
13 except:
14 print("Interval must be an integer between 1 and 60.")
15 get_seconds()
16
17 c = get_seconds()
18 print(c)
The idea is that if the user inputs Q or q, it quits immediately. If the user inputs a number between 1 and 60 inclusive, that value is returned. I'm running into the following issues though:
If I input all integers, only the first value is returned:
Run how often in seconds (1-60, q to Quit)? 99
Interval must be between 1 and 60.
Run how often in seconds (1-60, q to Quit)? -20
Interval must be between 1 and 60.
Run how often in seconds (1-60, q to Quit)? 5
99
If I use other data types causing int_a = int(a)
to fail, I get a return of None:
Run how often in seconds (1-60, q to Quit)? 5555.9999
Interval must be an integer between 1 and 60.
Run how often in seconds (1-60, q to Quit)? -5
Interval must be between 1 and 60.
Run how often in seconds (1-60, q to Quit)? 5
None
I'm having trouble tracking the contents of the variables through this. Basically the first variable that passes the 'int_a = int(a)' becomes what is returned, regardless of the recursion.
Any help is much appreciated!