I am trying to get the hour and mins from the user but when I enter a alphanumeric a number that exceeds the range the user it allowed the user returns a None type value. I am trying to just get the number from the user. I know the solution is obvious but for some reason I cant figure it out.
def get_hours():
h = input("Hour:")
try:
if len(h) == 0:
return 0
else:
h = int(h)
if 0 <= h <= 24:
print(h)
print(type(h))
return h
else:
print("Enter Hours between 0 and 24")
get_hours()
except ValueError:
print("Enter Hour example = 16")
get_hours()
def get_mins():
m = input("Minutes:")
try:
if len(m) == 0:
return 0
else:
m = int(m)
if 0 <= m <= 60:
print(m)
print(type(m))
return m
else:
print("Enter minutes between 0 and 60")
get_mins()
except ValueError:
print("Enter Minutes example = 23")
get_mins()
def get_activity():
flag = True
while flag:
ui = input("Enter a brief summary of what you will be doing? \n:")
if len(ui) == 0:
flag = True
else:
return ui
def main():
data = []
time = []
activity = []
hour = get_hours()
print(type(hour))
print("Hours: " +str(hour))
while hour == None:
hour = get_hours()
mins = get_mins()
print(type(mins))
print("Mins: " + str(mins))
while mins == None:
mins = get_mins()
main()
This is what I want:
Hours: 10
Minutes: 53
This is what I get when the following inputs are enterd
Hour:1a
Enter Hour example = 16
Hour:231
Enter Hours between 0 and 24
Hour:2
2
<class 'int'>
<class 'NoneType'>
Hours: None
Hour:2a
Enter Hour example = 16
Hour:2
2
<class 'int'>
Hour:2a
Enter Hour example = 16
Hour:2
2
<class 'int'>
Hour:2
2
<class 'int'>
Minutes:2a
Enter Minutes example = 23
Minutes:222
Enter minutes between 0 and 60
Minutes:2a
Enter Minutes example = 23
Minutes:2
2
<class 'int'>
<class 'NoneType'>
Mins: None
Minutes:2
2
<class 'int'>