假设两点:
- 这是一个创建口令的场景,目标是成功创建强口令;
- 创建者知道强口令的定义,即长度不小于8个字符,同时包含大写和小写字符,至少有一位数字。
那么:
- 无需限制尝试的次数;
- 无需使用print()提示强口令的定义。
import re
def detection(password):
upperCheckRegex = re.compile(r'[A-Z]+')
lowerCheckRegex = re.compile(r'[a-z]+')
digitCheckRegex = re.compile(r'[0-9]+')
if len(password) < 8:
return False
if upperCheckRegex.search(password) is None:
return False
if lowerCheckRegex.search(password) is None:
return False
if digitCheckRegex.search(password) is None:
return False
while True:
password = str(input('Creat a password:'))
if detection(password) == False:
print('Your password is not strong enough. Please try again.')
continue
else:
print('Your password has been created successfully.')
break