密码验证 - 至少一个大写,一个小写和一个数字

时间:2017-08-15 14:34:08

标签: python python-3.x validation

如何验证输入以检查其是否包含至少1个数字,一个大写字母和一个小写字母。我查看了类似的问题,但它们似乎并不是我所需要的,例如只检查数字和拒绝信件,我需要它,除了任何有一个以上的数字,大写字母和小写字母的内容。

2 个答案:

答案 0 :(得分:0)

您可以将assertany()string内置模块结合使用,如下所示:

import string

password = '1Aa'

try:
    assert any(i in string.ascii_lowercase for i in password)
    assert any(i in string.ascii_uppercase for i in password)
    assert any(i in string.digits for i in password)
except AssertionError as e:
    raise Exception('Invalid password!')

答案 1 :(得分:0)

import re
def passwordagain():
password()

def password():
        print()
        password = input("Enter a secure password: ")
        if   re.search('[A-Z]',password) is None:
            print("Password must contain at least one capital letter and one number.")
            passwordagain() 
        elif re.search('[0-9]',password) is None:
            print("Password must contain at least one capital letter and one number.")
            passwordagain() 
        elif re.search('[a-z]',password) is None: 
            print("Password must contain at least one capital letter and one number.")
            passwordagain()          
        else:
            filename = ("password.");
            with open (filename, "w") as f:
                f.write (password)
            print()
            print("Password saved")