python - 如何创建用于创建新用户名和密码的代码
问题描述
...其中密码必须满足以下条件:
- 超过 8 个字符
- 至少 1 个大写字母和 1 个小写字母、1 个数字和 1 个特殊符号
- 不包含用户名
def enterUsername():
username = input("Enter username: ")
username = enterUsername()
def enterPassword():
LENGTH = 8
password = input("Enter password: ")
upCase = False #indicate if password has at least 1 upper
lowCase = False #indicate if password has at least 1 lower
digit = False #indicate if password has at least 1 digit
special = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '_', '=']
for char in password: #iterate on each character of password (go through every character, for each character in the string)
if char.isupper(): #if character is in upper, set upCase to
upCase = True
if char.islower(): #if character is in lower, set lowCase to
lowCase = True
if char.isdigit(): #if character is a digit, set digit to
digit = True
for spechar in special:
if char == spechar:
special = True
length = len(password) #get length of password
strong = upCase and lowCase and digit and special and length > LENGTH #strong would be true if all the conditions hold
while True:
password = input("Enter password: ")
if not strong:
print("Your password is weak.")
else:
print ("Your password is strong enough.")
break
return password
enterPassword()
解决方案
这是一个适合我的测试仪:
password = input('Enter your password here : ')
print('\n')
from re import *
lower_match = compile(r'[a-z]').findall(password) # Finding if the password contains lowecase letters.
upper_match = compile(r'[A-Z]').findall(password) # Finding if the password contains uppercase letters.
symbol_match = compile(r'[|\"|\'|~|!|@|#|$|%|^|&|*|(|)|_|=|+|\||,|.|/|?|:|;|[|]|{\}|<|>]').findall(
password) # Finding if the password contains special characters.
number_match = compile(r'[0-9]').findall(password) # Finding if the password contains numbers.
if len(password) < 8 or len(lower_match) == 0 or len(upper_match) == 0 or len(symbol_match) == 0 or len(
number_match) == 0:
print('Your password is weak ! ')
elif len(password) >= 8:
print('Your password is strong ! ')
elif len(password) >= 16:
print('Your password is very strong ! ')
推荐阅读
- java - elki DBSCAN 的 distanceFunction 参数是什么?
- c# - C# 中的多重继承替换 - 属性中带有逻辑的特定假设示例
- java - 将字符串转换为 int 以将媒体时间转换为分钟和秒时出错
- vaadin - Vaadin 应用程序中的内存增长
- python - 如何使 TXT 文本中的字符串、变量在主代码中成为全局
- python-3.x - 为什么这个字符串不能转换成整数?Python
- laravel - “xxx.scss”不在 Laravel Mix 的 SourceMap 错误中
- for-loop - 在循环中选择行
- android - 在 android studio 调色板中找不到几个视图
- python - 从 json 中的列表返回值列表