python_校验密码是否合法

需求:校验密码是否合法的程序: 输入一个密码
    1、长度5-10位
    2、密码里面必须包含,大写字母、小写字母和数字

    3、最多输入5次

import re
import string

#list取交集实现
def check_input_bylist(pwd):
    if 5 < len(pwd) < 10:
        list1 = set(string.digits)
        list2 = set(string.ascii_lowercase)
        list3 = set(string.ascii_uppercase)
        if list1.intersection(pwd) and list2.intersection(pwd) and list3.intersection(pwd):
            return True
        else:
            return False
    else:
        return False

#正则实现
def check_input_byre(pwd):
    pattern=re.compile('^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{5,10})$')
    match = pattern.findall(pwd)
    if match:
        return True
    else:
        return False

#用isdigit()\islower()\isupper()实现
def check_input_byhanshu(pwd):
    if 5 < len(pwd) < 10:
        num_count=0
        lower_count=0
        upper_count=0
        for p in pwd:
            if p.isdigit():
                num_count+=1
            elif p.islower():
                lower_count+=1
            elif p.isupper():
                upper_count+=1
        if num_count>0 and lower_count>0 and upper_count>0:
            return True
        else:
            return False



for i in range(5):
    passwd = input("请输入密码:").strip()
    if check_input_bylist(passwd) == True:  #这里分别使用check_input_bylist()、check_input_byre()、check_input_byhanshu()方法都可以
        print('通过密码校验!')
        break
    else:
        print('密码长度限制5-10位, 且须包含,大写字母、小写字母和数字!')
else:
    print('失败次数过多!')

猜你喜欢

转载自blog.csdn.net/sylvia2016/article/details/79926172
今日推荐