python编程实现密码检测

假定某系统密码设定规则如下:
(1). [a-z]之间至少有1个字母 【判断条件提示:j >= 'a' and j <= 'z',以下类似】
(2). [A-Z]之间至少有1个字母
(3). [0-9]之间至少有1个数字
(4). [@#$%&]中至少有2个字符
(5).密码的最短长度:7
(6).密码的最大长度:13
(7).密码不能包含空格
请编程实现下述问题:程序接受键盘输入的多个逗号分隔的密码,并根据上述密码设定规则对所有密码进行检测,将符合条件的密码以字符串形式打印,每个密码用逗号分隔。
例:
如果以下密码作为程序的输入:ABd1#234@1,a F1#@12,2w&a3E
,2We3345,dHuD&u12=e, ABd1234@1,则程序的输出应该是:符合密码设定标准的结果为:ABd1#234@1,2w&a3E

代码如下,望采纳,谢谢!

def check_password(password):
    # fill your codes after this line
    s=[0,0,0,0]
    for c in password:
        if c >= '0' and c <= '9':
            s[0]=1
        if c >= 'A' and c <= 'Z':
            s[1]=1
        if c >= 'a' and c <= 'z':
            s[2]=1
        if c in '@#$%&':
            s[3]+=1
        if c == ' ':
            return False
    if sum(s) >= 5 and s[3] >= 2 and len(password) >= 7 and len(password) <= 13:
        return True
    else:
        return False
    # end of your codes
# do not modify
if __name__ == "__main__":
    passwords = input().split(',')
    oks=[]
    for password in passwords:
        if check_password(password):
            oks.append(password)
    print("符号密码设定标准的结果为:",','.join(oks))

img

提供参考代码:

import re
print('请输入密码:')
password=input().split(',')
value=[]
for p in password:
    if len(p)<6 or len(p)>12:
        continue
    if not re.search('[a-z]',p):
        continue
    elif not re.search('[0-9]',p):
        continue
    elif not re.search('[A-Z]',p):
        continue
    elif not re.search('[@#$]',p):
        continue
    else:
        pass
    value.append(p)
print(','.join(value))

经过整理调试,得到了相应的代码,过程不易,希望能够对你有帮助!

如果感觉有用的话请采纳!


print('请输入密码,并用逗号隔开:')
str1 = input()
words = [x for x in str1.split(',')]
print(words)
result = []
for word in words:
    n1 = 0
    n2 = 0
    n3 = 0
    n4 = 0
    n5 = 0
    print(word)
    if 6 < len(word) < 14:
        for i in word:
            if "0" <= i <= "9":
                n1 += 1
            if "a" <= i <= "z":
                n2 += 1
            if "A" <= i <= "Z":
                n3 += 1
            if i in "@#$%&":
                n4 += 1
            if i.isspace():
                n5 += 1
        if n1 >= 1 and n2 >= 1 and n3 >= 1 and n4 >= 2 and n5 == 0:
            result.append(word)

print(result)
print(','.join(result))