验证密码 - 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()
相关文章