密码验证-Python [英] Validation of a Password - Python

查看:172
本文介绍了密码验证-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have to create code that validate whether a password:

  • 长度至少为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 capital letter in it")
        else:
            print("Your password seems fine")
            break

validate()

我不确定这是怎么回事,但是当我输入包含数字的密码时-它会不断告诉我我需要一个包含数字的密码.有解决方案吗?

I'm not sure what is wrong, but when I enter a password that has a number - it keeps telling me that I need a password with a number in it. Any solutions?

推荐答案

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

You can use re module for regular expressions.

有了它,您的代码将如下所示:

With it your code would look like this:

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()

这篇关于密码验证-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆