系统密码长度为6-12个字符
我刚接触Python,最近被分配了一个任务,要完成以下内容:
设计、编写、测试和评估一个系统,用来接受和检查密码是否符合某些要求:
- 密码长度必须在6到12个字符之间。
- 系统需要告诉用户密码不合格的原因,并要求用户重新输入,直到输入一个合格的密码为止。
- 需要显示一条消息,告诉用户密码是可以接受的。
- 可以根据简单的标准来评估密码的强度,比如说,如果一个密码只使用大写字母、小写字母和数字中的某一种,可以这样评估密码的强度:
- 如果只用了一种类型,比如全是小写字母或全是数字,那就是弱密码(WEAK)。
- 如果用了两种类型,那就是中等密码(MEDIUM)。
- 如果三种类型都用了,那就是强密码(STRONG)。
到目前为止,我做了以下工作,但还没能正确运行:
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')
但还是无法正常工作,请帮帮我!!!
5 个回答
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:
>>> 'a123'.lower() == 'a123' and 'a123'.upper() == 'a123'
False
>>> '1234'.isalnum() and '1234'.upper() == '1234'
True
因为小写和大写必须同时为真,或者字母数字和大写字母也必须同时满足条件——但是像A12345或12345这样的密码被认为是弱密码,所以它们不能触发中等强度的检查...
你可以考虑为你的密码设计一个测试套件。
试着用正则表达式来检查你的密码,看看它是否符合各种条件。这样可以帮助你找到匹配的密码。你可以参考这个链接:
你的代码有点难读。我建议把密码的实际测试部分分成一个单独的函数,就像下面的程序那样。
这样你就能更清楚地看到在测试什么,并且可以在主函数中对测试结果做出反应。
我使用一个密码类来存储测试结果,因为我觉得这样看起来更整洁。
下面是一个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
好吧,您遇到的具体问题不太清楚,但您检查密码强度是否为中等的条件有点马虎。
elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:
这里有个建议,可以考虑使用布尔变量。
D -> 字符串中至少包含1个数字。
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
密码被认为强的条件只有一个。
您可以通过使用正则表达式来计算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")
这可能看起来有点复杂,但这是处理这个问题更系统的方法。想想如果您还要包括特殊字符和其他要求该怎么做!