检查Python中的所有条件,即使条件为true,也要检查其他条件 [英] Check every condition in Python if else even if one evaluates to true

查看:78
本文介绍了检查Python中的所有条件,即使条件为true,也要检查其他条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中是否有一种方法可以继续检查if else语句的条件(如果结果为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

例如,如果密码没有大写字母或数字,我想同时打印两条消息,而不仅是密码必须包含大写字母",它也就此结束.我试图通过从每个语句中取出return来解决此问题,但是它没有用.任何帮助表示赞赏,谢谢.

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.

推荐答案

不要使用elif,而使用if将确保检查条件.仅当if语句的评估结果为False

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已经将状态设置为True,因此只需return status无需else

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

这篇关于检查Python中的所有条件,即使条件为true,也要检查其他条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆