我是Python世界的新手,被赋予了完成以下任务的任务:
设计、编码、测试和评估一个系统,以接受和测试某些特征的密码:
- 至少6个字符,长度不超过12个字符。
- 系统必须提示密码失败及其原因,要求用户重新输入他们的选择,直到输入成功的密码。
- 必须显示可接受密码的消息。
- 密码强度可以根据简单的标准来评估其适用性;例如,只使用大小写字母和数字字符的密码系统可以评估密码强度如下:
- WEAK如果只使用一种类型,例如全小写或全数字
- MEDIUM如果使用两种类型
- STRONG
到目前为止,我已经完成了以下操作,但没有使其正常工作:
def password():
print ('Welcome user ! Please enter password belown')
print ('The password entered must be between 6-12 characters longn')
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 okn')
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:
建议考虑布尔变量
D ->至少包含1位数字的字符串
U -> string包含至少1个大写字母和
L->包含至少一个小写字符的字符串
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很容易计算
现在减少你的表达式
strong = D * U * L
medium = (!)D * U * L) + (D * !U * L) + (D * U * !L)
low = (!)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")
这可能看起来有点难看,但这是一个更系统的方法来处理这个问题,想想如果你要包含特殊字符和其他要求,你会怎么做!
同意这是正则表达式的一个很好的用例。
也好像你的中大小写永远不会被触发:
elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:
>>> 'a123'.lower() == 'a123' and 'a123'.upper() == 'a123'
False
>>> '1234'.isalnum() and '1234'.upper() == '1234'
True
因为lower和upper必须同时为真或者isalnum和isalupper必须同时被触发->但是A12345或12345被认为是弱的,所以它不能触发med…
你可以考虑为你的密码做一套测试服。
尝试根据各种条件检查您的密码,使用正则表达式搜索和匹配密码。你可能会发现这个链接很有用:
包含多个条件语句的密码检查器
你的代码有点难读。我会将密码的实际测试拆分为一个单独的函数,如下面的程序所示。
然后您可以更清楚地看到正在测试的内容,并且可以在主函数中对测试结果作出反应。
我使用一个密码类来存储测试结果,因为我喜欢它看起来很干净。
Python示例3:
import re
def check_password(chars, min_chars=6, max_chars=12):
class Password: pass
Password.has_uppercase = bool(re.search(r'[A-Z]', chars))
Password.has_lowercase = bool(re.search(r'[a-z]', chars))
Password.has_numbers = bool(re.search(r'[0-9]', chars))
if not min_chars <= len(chars) <= max_chars:
print("Password needs to be between %s and %s characters." % (min_chars, max_chars))
return
/* Return a list of only my attributes
of the Password class. */
return [t for t in vars(Password).items() if not t[0].startswith("__")]
if __name__ == "__main__":
meanings = {0: "unacceptable", 1: "Weak", 2: "Medium", 3: "Strong"}
while True:
chars = input('nEnter a password: ')
results = check_password(chars)
if results:
/* Points are equal to how many tests are successful */
points = len([p[0] for p in results if p[1] is True])
/* Print out the results to the console */
print("nPassword strength is %s.n" % meanings.get(points))
for condition, result in results:
print("{0:<15} {1} {2}".format(condition, ":", bool(result)))
break
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')