简体   繁体   English

if 语句中的 else 分支无法正常工作(python)

[英]The else branch in the if statement doesnt work properly (python)

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.当我输入像“9”这样的数字时,我希望结果将是“仅接受 1 ~ 6 之间的数字”,但 else 语句实际上被忽略并通过。

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 $ py main.py

Select number below Select 编号如下

(1)12:00~13:30 (1)12:00~13:30

(2)13:35~15:05 (2)13:35~15:05

(3)15:45~17:15 (3)15:45~17:15

(4)17:20~18:50 (4)17:20~18:50

(5)18:55~20:30 (5)18:55~20:30

(6)20:25~22:00 (6)20:25~22:00


$ 9 9 美元

Leave a comment if you have有的话请留言

$ it failed $ 它失败了

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.它有效,但看起来仍然有些多余。

Syntax for re.match(pattern, string) re.match(pattern, string) 的语法

So the code re.match(r'[1-6]', class_number) is always throwing an exception.所以代码 re.match(r'[1-6]', class_number) 总是抛出异常。

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') class_number = input( '选择下面的数字\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('如果有请留言\n')

======== ========

9 Leave a comment if you have 343 <class 'str'> Only accepted the numbers between 1 ~ 6 9 如果你有 343 发表评论 <class 'str'> 只接受 1 ~ 6 之间的数字

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.在编写代码时,您的re.match始终返回True ,因此永远不会到达您的else代码。

If you want to catch bad class_number immediately, then consider:如果您想立即捕获错误的class_number ,请考虑:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM