I expect the result will be 'Only accepted the numbers between 1 ~ 6' when I input some number like '9', but the else statement is actually ignored and get through.
main.py
def select_class():
while True:
try:
class_number = int(input('Select number below\n (1)12:00~13:30\n (2)13:35~15:05\n (3)15:45~17:15\n (4)17:20~18:50\n (5)18:55~20:30\n (6)20:25~22:00\n---------------\n'))
class_text = input('Leave a comment if you have\n')
if re.match(r'[1-6]', class_number):
if class_number == 1:
class_hr, class_min = '00', '00'
return class_hr, class_min, class_text
elif class_number == 2:
class_hr, class_min = '01', '35'
return class_hr, class_min, class_text
elif class_number == 3:
class_hr, class_min = '03', '45'
return class_hr, class_min, class_text
elif class_number == 4:
class_hr, class_min = '05', '20'
return class_hr, class_min, class_text
elif class_number == 5:
class_hr, class_min = '06', '55'
return class_hr, class_min, class_text
elif class_number == 6:
class_hr, class_min = '08', '30'
return class_hr, class_min, class_text
else:
print('Only accepted the numbers between 1 ~ 6')
return class_number
except ValueError:
pass
terminal:
$ py main.py
Select number below
(1)12:00~13:30
(2)13:35~15:05
(3)15:45~17:15
(4)17:20~18:50
(5)18:55~20:30
(6)20:25~22:00
$ 9
Leave a comment if you have
$ it failed
ifwith there.matchclause? You could have simply appended theelseat the end of your secondif/elifchain (and delete the firstif).class_numberis an integer, sore.match()ought to throw aTypeErrorexception. Also, the indentation is incorrect as shown. Please fix the question, so we know what code you're actually running.class_number) which should always generate a TypeError. BTW thatif re.match...if not needed at all, you just have to tie the else: to the end of the elifs.