验证密码 – Python

所以我必须创建验证密码是否的代码:

>至少8个字符
>至少包含1个数字
>包含至少1个大写字母

这是代码:

def validate():
    while True:
        password = input("Enter a password: ")
        if len(password) < 8:
            print("Make sure your password is at lest 8 letters")
        elif not password.isdigit():
            print("Make sure your password has a number in it")
        elif not password.isupper(): 
            print("Make sure your password has a number in it")
        else:
            print("Your password seems fine")
            break

validate()

我不确定有什么问题,但是当我输入一个有号码的密码时 – 它一直告诉我我需要一个带有数字的密码.有解决方案吗

解决方法:

您可以将re模块用于正则表达式.

有了它,你的代码看起来像这样:

import re

def validate():
    while True:
        password = raw_input("Enter a password: ")
        if len(password) < 8:
            print("Make sure your password is at lest 8 letters")
        elif re.search('[0-9]',password) is None:
            print("Make sure your password has a number in it")
        elif re.search('[A-Z]',password) is None: 
            print("Make sure your password has a capital letter in it")
        else:
            print("Your password seems fine")
            break

validate()
上一篇:java – 如何阅读使用未知的随机所有者密码创建的PDF?


下一篇:java – 有没有替代JPasswordField?