即使一个评估为true,也要检查Python中的每个条件

时间:2014-08-08 20:42:52

标签: python if-statement

如果评估为true,Python中有没有办法继续检查if else语句的条件?这是我的代码:

status = True

  if pass_len(S) == False:
    print ('Password must be at least 6 characters long')
    status = False
  elif pass_upper(S) == False:
    print('Password must include upper case letters')
    status = False
  elif pass_lower(S) == False:
    print('Password must include lower case letters')
    status = False
  elif pass_nums(S) == False:
    print('Password must include digits.')
    status = False
  else:
    status = True

  return status

例如,如果密码没有大写字符或数字,我想打印这两个消息,而不仅仅是“密码必须包含大写字母”,它就在那里结束。我尝试通过退回每个声明来解决这个问题,但它没有奏效。任何帮助表示赞赏,谢谢。

3 个答案:

答案 0 :(得分:8)

请勿使用elif,使用if将确保检查条件。只有在elif语句评估为if

时才会检查False
In [40]: foo = 3

In [41]: if foo == 3:
   ....:     print (foo)
   ....: if foo != 4:    
   ....:     print ("checked too")
   ....:     
3
checked too

status = True已将状态设置为True,因此return status无需else

答案 1 :(得分:5)

执行此操作的另一种方法是缩短一点,并且还列出了触发的错误列表:列表理解:

errors = [message for (has_error, message) in (
    (not pass_len(S), 'Password must be at least 6 characters long'),
    (not pass_upper(S), 'Password must include upper case letters'),
    (not pass_lower(S), 'Password must include lower case letters'),
    (not pass_nums(S), 'Password must include digits.'),
    ) if has_error]

答案 2 :(得分:1)

从@JasonS的优秀答案中恶毒地窃取,这是我在评论中提到的一种比较笨拙的方法。

# :: all functions look just like this one! ::
import functools, operator

def pass_len(password, length=6):
    try:
        if len(password) < length:
            return 0b0001
        else:
            return 0
    except Exception as e:
        return 0b0001
# pass_upper returns 0b0010 or 0
# pass_lower returns 0b0100 or 0
# pass_nums  returns 0b1000 or 0

def password_validate(password):
    conditions = [pass_len, pass_upper, pass_lower, pass_nums]
    result = functools.reduce(operator.or_, map(lambda x: x(password), conditions))
    errors = [message for (bitflag, message) in (
        (0b0001, 'Password must be at least 6 characters long'),
        (0b0010, 'Password must include upper case letters'),
        (0b0100, 'Password must include lower case letters'),
        (0b1000, 'Password must include digits.')
        ) if result & bitflag]
    for error in errors:
        print(error)

输出:

>>> password_validate("hunter12")
Password must include upper case letters
>>> password_validate("HUNTER")
Password must include lower case letters
Password must include digits.
>>> password_validate('')
Password must be at least 6 characters long
Password must include upper case letters
Password must include lower case letters
Password must include digits.

您可以通过以下方式进一步干掉:

def password_validator(bitflag):
    def wrap(f):
        def wrapped_f(*args):
            return bitflag if f(*args) else 0
        return wrapped_f
    return wrap

@password_validator(0b0001)
def pass_len(password, length=6):
    return len(password) < length
@password_validator(0b0010)
def pass_upper(password):
    return password.lower() == password
@password_validator(0b0100)
def pass_lower(password):
    return password.upper() == password
@password_validator(0b1000)
def pass_num(password):
    return any(num in password for num in '0123456789')

def password_validate(password):
    # this function is identical to the one above