使用正则表达式时如何连接多个if语句?

时间:2016-08-20 23:44:55

标签: python python-2.7

以下是密码验证过程的代码,至少有一个小写,一个大写和一个数字。

md-delay

我希望让Python运行,如果import re password=raw_input('Enter the Password') x= (re.findall(r'[a-z]',password)) if len(x)==0: print " at least one 'a-z' requirment not completed" else: #(what should i write here to connect this to the next step?) y=(re.findall(r'[A-Z',password)) if len(y)==0: print "at least one 'A-Z' requirement not completed" else: #(what should i write here?) z=(re.findall(r'[0-9]',password)) if len(z)==0: print "at least one '0-9' requirment not completed" else: print ' Good password!' 不为0,那么请继续验证x是否等于0.如果不是,请验证y不等于0.如果不是,请告诉用户密码是否正常。

4 个答案:

答案 0 :(得分:2)

使用if-elif声明:

import re
password=raw_input('Enter the Password')

if len(re.findall(r'[a-z]',password))==0:
    print " at least one 'a-z' requirment not completed"
elif len(re.findall(r'[A-Z]',password)) == 0:
    print "at least one 'A-Z' requirement not completed"
elif len(re.findall(r'[0-9]',password)) == 0:
    print "at least one '0-9' requirment not completed"
else:
    print ' Good password!'

演示:

Enter the Password  Axa3
 Good password!

Enter the Password  aa3
at least one 'A-Z' requirement not completed

Enter the Password  A3443E
 at least one 'a-z' requirment not completed

答案 1 :(得分:1)

import re
password=raw_input('Enter the Password: ')
problems = []

x = re.findall(r'[a-z]',password)
if not x:
   problems.append("at least one 'a-z' requirment not completed")

y = re.findall(r'[A-Z]',password)
if not y:
   problems.append("at least one 'A-Z' requirement not completed")

z = re.findall(r'[0-9]',password)
if not z:
   problems.append("at least one '0-9' requirment not completed")

if problems:
    print ' There were some problems with your password: ' + ', '.join(problems)
else:
    print ' Good password!'

答案 2 :(得分:1)

这是一种只使用字符串方法的方法!

p = raw_input('enter password')
if p.islower() or p.isupper() or not any(c.isdigit() for c in p):
    print('bad password')

答案 3 :(得分:0)

基本上,你的else子句就是下一个语句,就像这样。你几乎写了它,所有你需要做的就是缩进每个检查。

    import re
    password=raw_input('Enter the Password')

    x= (re.findall(r'[a-z]',password))
    if len(x)==0:
         print " at least one 'a-z' requirment not completed"
    else:
        y=(re.findall(r'[A-Z',password))
        if len(y)==0:
             print "at least one 'A-Z' requirement not completed"
        else:
            z=(re.findall(r'[0-9]',password))

            if len(z)==0:
                 print "at least one '0-9' requirment not completed"
            else:
                print ' Good password!'

选项2:

    import re
    password=raw_input('Enter the Password')

    if not (re.findall(r'[a-z]',password)):
         print " at least one 'a-z' requirment not completed"
    elif not (re.findall(r'[A-Z',password)):
        print "at least one 'A-Z' requirement not completed"
    elif not (re.findall(r'[0-9]',password)):
        print "at least one '0-9' requirment not completed"
    else:
        print ' Good password!'