我的问题是设计一个Python脚本,要求用户输入密码,然后让Python验证密码是否适合该条件。
以下是用户输入密码的条件:
如果条件匹配,则输出是。否则,不会。
这些是我尝试过的:
from sys import exit
def check_alpha(input):
alphas = 0
alpha_list = "A B C D E F G H I J K L M N I O P Q R S T U V W X Y Z".split()
for char in input:
if char in alpha_list:
alphas += 1
if alphas > 0:
return True
else:
return False
def check_number(input):
numbers = 0
number_list = "1 2 3 4 5 6 7 8 9 0".split()
for char in input:
if char in number_list:
numbers += 1
if numbers > 0:
return True
else:
return False
def check_special(input):
specials = 0
special_list = "_-"
for char in input:
if char in special_list:
specials += 1
if specials > 0:
return True
else:
return False
def check_len(input):
if len(input) >= 6:
return True
else:
return False
def validate_password(input):
check_dict ={
'alpha':check_alpha(input),
'number':check_number(input),
'special':check_special(input),
'len':check_len(input)
}
if check_alpha(input) & check_number(input) & check_sprcial(input) & check_len(input)
return True
else:
print"No"
while True:
password = raw_input("Enter password:")
print
if validate_password(password):
print("Yes")
else
print("No")
或者:
import re
while True:
user_input = input("Please enter password:")
is_valid = False
if(len(user_input)<6):
print("No")
continue
elif not re.search("[a-z]",user_input):
print("No")
continue
elif not re.search("[0-9]",user_input):
print("No")
continue
elif re.search("[~!@#$%^&*`+=|\;:><,.?/]",user_input):
print("No")
continue
else:
is_valid = True
break
if(is_valid):
print("Yes")
答案 0 :(得分:0)
我建议您看看getpass
模块。为帮助您入门,请看以下链接:getpass (examples series 1)和examples series 2
答案 1 :(得分:0)
尝试一下:
import re
pw = raw_input('Type a password: ') # get input from user
if any([not pw[0].isalpha(), # check if first char is a letter
len(pw) < 6, # check if len is greater than or equal to 6
not re.match(r'^[\w-]*$', pw)]): # check if all chars are alphanumeric, underscores, or dashes
print 'No'
else:
print 'Yes'
一些测试用例的示例输出:
Type a password: qwer
No
Type a password: qwerty
Yes
Type a password: 1a2b3c
No
Type a password: ASDF1234!!!!
No
Type a password: a.a.a.a
No
答案 2 :(得分:0)
您可以将3个条件合为一行,并避免使用变量is_valid
。您还错过了第一个字符的条件:
import re
user_input = raw_input('Please enter password:')
if len(user_input)>=6 and user_input[0].isalpha() and re.match(r"^[\w-]*$", user_input):
print('Yes')
else:
print('No')
答案 3 :(得分:0)
import re
def validate(password):
if len(password) < 6 or re.search('^[A-Za-z][A-Za-z0-9-_]*$',password) is None:
print("password not accepted")
else:
print("Your password seems fine")
答案 4 :(得分:0)
Container
答案 5 :(得分:-1)
您的问题的理想解决方案是正则表达式。尝试在前端进行验证。 类似于javascript。
据您所知,请检查Python文档中的以下链接。 https://docs.python.org/3/howto/regex.html
答案 6 :(得分:-1)
Import re
Password = (input("enter password here:"))
flag = 0
while True:
if (len(password)<7):
flag = -1
break
elif not re.search("[a-z]", password):
flag = -1
break
elif not re.search("[A-Z]", password):
flag = -1
break
elif not re.search("[0-9]", password):
flag = -1
break
elif not re.search("[#@$&*_]", password):
flag = -1
break
else:
flag = 0
print("strong")
break
if flag == -1:
print("weak")