python 中条件合法的_检查Python中的所有条件,即使其他条件评估为true

Is there a way in Python to continue checking conditions of an if else statement if one evaluates to true? Here's my code:

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

For example, if the password doesn't have an uppercase character or numbers, I'd like to print both messages, instead of just "Password must include upper case letters" and it ending right there. I tried getting around this by taking return out of each statement, but it didn't work. Any help is appreciated, thanks.

解决方案

Don't use elif, using if will make sure the the condition is checked. elif will only be checked when the if statement evaluates to False

In [40]: foo = 3

In [41]: if foo == 3:

....: print (foo)

....: if foo != 4:

....: print ("checked too")

....:

3

checked too

status = True sets status to True already so just return status no need for an else

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值