我是 Python 世界的新手,任务是完成以下任务:
设计、编码、测试和评估一个系统来接受和测试密码的某些特征:
- 长度至少应为 6 个字符,且不超过 12 个字符。
- 系统必须指出密码失败的原因,要求用户重新输入他们的选择,直到输入成功的密码。
- 必须显示一条表明密码可接受的消息。
- 可以根据简单的标准评估密码强度,以评估其适用性;例如,仅使用大小写字母字符和数字字符的密码系统可以将密码强度评估为:
- 如果只使用一种类型,则为 WEAK,例如全部小写或全部数字
- MEDIUM 如果使用两种类型
- 如果使用所有三种类型,则强。
到目前为止,我已经完成了以下操作,但无法正常工作:
def password():
print ('Welcome user ! Please enter password below\n')
print ('The password entered must be between 6-12 characters long\n')
while True:
password = input ('Please enter your password . . . :')
weak = 'weak'
med = 'medium'
strong = 'strong'
if len(password) >12:
print ('password is too long It must be between 6 and 12 characters')
elif len(password) <6:
print ('password is too short It must be between 6 and 12 characters')
elif len(password) >=6 and len(password) <= 12:
print ('password ok\n')
if password.lower()== password or password.upper()==password or password.isalnum()==password:
print ('password is', weak)
elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:
print ('password is', medium)
else:
password.lower()== password and password.upper()==password and password.isalnum()==password
print ('password is', strong)
break
password()
我尝试引入一个 while
循环:
while invalid:
if len(password) >=6 and (password) <=12:
password=False
# number in range
# sets invalid to False to stop loop
else:
print('Sorry the password you entered was not between 6 and 12 characters long')
print('Please try again')
print('You have entered a valid password')
但仍然无法正常工作请帮忙!!!
最佳答案
好吧,不清楚你面临的具体问题但是你检查密码强度的条件很草率
elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:
这是一个考虑 bool 变量的建议
D -> 至少包含一位数字的字符串
U -> 包含至少 1 个大写字母和的字符串
L->至少包含1个小写字符的字符串
D | U | L == low | medium | strong
0 0 0 1
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1
只有一种方式可以将密码视为强密码
您可以使用 regex 计算 D
_digits = re.compile('\d')
def contains_digits(d):
return bool(_digits.search(d))
条件U和L很容易计算
现在在减少你的表情之后
强 = D * U * L
中等 = (!D * U * L) + (D * !U * L) + (D * U * !L)
低 = (!D * !U) + (!D * !L) + (!U * !L)
所以你的代码看起来像
D = areDigits(Password)
U = areUpper(Password)
L = areLower(Password)
if((!D and !U) or (!D and !L) or (!U and !L)):
print("weak password")
elif(D and U and L):
print("strong password")
else:
print("medium strength password")
这可能看起来有点难看,但它是一种更系统的处理方式 想一想如果您要包含特殊字符和其他要求,您会怎么做!
关于python - 系统密码长度在 6-12 个字符之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24881554/