0

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

6
  • 1
    Why do you need the first if with the re.match clause? You could have simply appended the else at the end of your second if/elif chain (and delete the first if). Commented Mar 4, 2021 at 3:56
  • 1
    Are you using same indentation in your actual code?. It failed is also input or some output of your code? Commented Mar 4, 2021 at 3:59
  • 1
    class_number is an integer, so re.match() ought to throw a TypeError exception. Also, the indentation is incorrect as shown. Please fix the question, so we know what code you're actually running. Commented Mar 4, 2021 at 4:03
  • Does that code ever work. You're calling re.match on an integer (class_number) which should always generate a TypeError. BTW that if re.match... if not needed at all, you just have to tie the else: to the end of the elifs. Commented Mar 4, 2021 at 4:26
  • 'It failed' is a comment. it maybe confused you. Commented Mar 4, 2021 at 4:44

3 Answers 3

1

I modified the code to the one below:

def select_class():

while True:
 try:
  class_number = 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')

  if int(class_number) == 1:
    class_hr, class_min = '00', '00'
    class_text = input('Leave a comment if you have\n')
    return class_hr, class_min, class_text
  elif int(class_number) == 2:
    class_hr, class_min = '01', '35'
    class_text = input('Leave a comment if you have\n')
    return class_hr, class_min, class_text
  elif int(class_number) == 3:
    class_hr, class_min = '03', '45'
    class_text = input('Leave a comment if you have\n')
    return class_hr, class_min, class_text
  elif int(class_number) == 4:
    class_hr, class_min = '05', '20'
    class_text = input('Leave a comment if you have\n')
    return class_hr, class_min, class_text
  elif int(class_number) == 5:
    class_hr, class_min = '06', '55'
    class_text = input('Leave a comment if you have\n')
    return class_hr, class_min, class_text
  elif int(class_number) == 6:
    class_hr, class_min = '08', '30'
    class_text = input('Leave a comment if you have\n')
    return class_hr, class_min, class_text
  else:
    print('Only accepted the numbers between 1 ~ 6')
    continue
except ValueError:
  pass

It works but still looks redundant somewhat.

Sign up to request clarification or add additional context in comments.

2 Comments

This is actually a bad answer: you should cast int once, you should narrow try block, you duplicate two lines of code in all 6 of your checks, and you have entangled class_number verification with class_text verification. This is a code maintainer's nightmare.
This is pretty much that you pointed out. I couldn't hit on a right code when there are a number of conditional branches. I read your answer once again and understood how to use a while statement properly for now.
0

Your match needs to match against a string.

import re
while True:
    class_number = input('Select number ...')
    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
        # ...
    else:
        print('Only accepted the numbers between 1 ~ 6')

As your code is written, your re.match always returns True, so your else code is never reached.

If you want to catch bad class_number immediately, then consider:

while True:
    class_number = ''
    while True:
        class_number = input('Select number: '))
        if re.match(r'[1-6]', class_number):
            break
        print('Only accept numbers 1 through 6')
    class_text = input('Leave a comment if you have: \n')
    if class_number == '1':
        class_hr, class_min = '00', '00'
    elif class_number == 2:
        class_hr, class_min = '01', '35'
    elif class_number == 3:
        class_hr, class_min = '03', '45'
    elif class_number == 4:
        class_hr, class_min = '05', '20'
    elif class_number == 5:
        class_hr, class_min = '06', '55'
    elif class_number == 6:
        class_hr, class_min = '08', '30'
    return class_hr, class_min, class_text

3 Comments

I guess class_text = input('Leave a comment if you have\n') should be placed somewhere else after the if statements, right?
If choosing between a string vs int representation, you should take into consideration how you want to use the data to decide. Since we rely on regular expression here (which operates exclusively on strings) then let's pick string. Besides, we started with string to begin with. Casting to integer int(input('')) is a very common way to quickly sanity check data. But it throws an Error which forces us int a try block. It offers nothing that the regular expression check does not already do.
I will keep it mind that int(input(' ')) throws an error which forces us int a try block. I modified my code which you can find the answer below. It finally work but looks not smart.
0

Syntax for re.match(pattern, string)

So the code re.match(r'[1-6]', class_number) is always throwing an exception.

Change your code to below

class_number = 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')

========

9 Leave a comment if you have 343 <class 'str'> Only accepted the numbers between 1 ~ 6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.