Python基础(八) 判断密码强弱demo
判断密码强弱1.0
https://docs.python.org/3/library/stdtypes.html#string-methods
'''
判断密码强弱1.0
'''
def check_number_exist(password_str):
"""
判断字符串中是否含有数字
"""
for c in password_str:
if c.isnumeric():
return True
return False
def check_letter_exist(password_str):
"""
判断字符串中是否含有字母
"""
for c in password_str:
if c.isalpha():
return True
return False
def main():
"""
主函数
"""
password = input('请输入密码:')
# 密码强度
strength_level = 0
# 规则1:密码长度大于8
if len(password) >= 8:
strength_level += 1
else:
print('密码长度要求至少8位!')
# 规则2:包含数字
if check_number_exist(password):
strength_level += 1
else:
print('密码要求包含数字!')
# 规则3:包含字母
if check_letter_exist(password):
strength_level += 1
else:
print('密码要求包含字母!')
if strength_level == 3:
print('恭喜!密码强度合格!')
else:
print('密码强度不合格!')
if __name__ == '__main__':
main()
测试结果1:
请输入密码:#
密码长度要求至少8位!
密码要求包含数字!
密码要求包含字母!
密码强度不合格!
测试结果2:
请输入密码:asdasd123
恭喜!密码强度合格!
判断密码强弱2.0
'''
判断密码强弱2.0
'''
def check_number_exist(password_str):
"""
判断字符串中是否含有数字
"""
has_number = False
for c in password_str:
if c.isnumeric():
has_number = True
break
return has_number
def check_letter_exist(password_str):
"""
判断字符串中是否含有字母
"""
has_letter = False
for c in password_str:
if c.isalpha():
has_letter = True
break
return has_letter
def main():
"""
主函数
"""
try_times = 5
while try_times > 0:
password = input('请输入密码:')
# 密码强度
strength_level = 0
# 规则1:密码长度大于8
if len(password) >= 8:
strength_level += 1
else:
print('密码长度要求至少8位!')
# 规则2:包含数字
if check_number_exist(password):
strength_level += 1
else:
print('密码要求包含数字!')
# 规则3:包含字母
if check_letter_exist(password):
strength_level += 1
else:
print('密码要求包含字母!')
if strength_level == 3:
print('恭喜!密码强度合格!')
break
else:
print('密码强度不合格!')
try_times -= 1
print()
if try_times <= 0:
print('尝试次数过多,密码设置失败!')
if __name__ == '__main__':
main()
测试结果:
尝试次数过多,密码设置失败!
测试结果2:
请输入密码:asdasd123
恭喜!密码强度合格!
判断密码强弱3.0
测试结果:
请输入密码:asdasd123
恭喜!密码强度合格!
请输入密码:asdasd123
恭喜!密码强度合格!
请输入密码:aa
密码长度要求至少8位!
密码要求包含数字!
密码强度不合格!
判断密码强弱4.0
'''
判断密码强弱4.0
'''
def check_number_exist(password_str):
"""
判断字符串中是否含有数字
"""
has_number = False
for c in password_str:
if c.isnumeric():
has_number = True
break
return has_number
def check_letter_exist(password_str):
"""
判断字符串中是否含有字母
"""
has_letter = False
for c in password_str:
if c.isalpha():
has_letter = True
break
return has_letter
def main():
"""
主函数
"""
# 读取文件
f = open('password_3.0.txt', 'r')
# 1. read()
# content = f.read()
# print(content)
# 2. readline()
# line = f.readline()
# print(line)
# line = f.readline()
# print(line)
# line = f.readline()
# print(line)
# 3. readlines()
for line in f:
print('read: {}'.format(line))
f.close()
if __name__ == '__main__':
main()
测试结果1:
密码:asdasd123, 强度:3
密码:asdasd123, 强度:3
密码:aa, 强度:1
测试结果2:
密码:asdasd123, 强度:3
密码:asdasd123, 强度:3
密码:aa, 强度:1
测试结果3:
密码:asdasd123, 强度:3
密码:asdasd123, 强度:3
密码:aa, 强度:1
判断密码强弱5.0
'''
判断密码强弱5.0
'''
class PasswordTool:
"""
密码工具类
"""
def __init__(self, password):
# 类的属性
self.password = password
self.strength_level = 0
def process_password(self):
# 规则1:密码长度大于8
if len(self.password) >= 8:
self.strength_level += 1
else:
print('密码长度要求至少8位!')
# 规则2:包含数字
if self.check_number_exist():
self.strength_level += 1
else:
print('密码要求包含数字!')
# 规则3:包含字母
if self.check_letter_exist():
self.strength_level += 1
else:
print('密码要求包含字母!')
# 类的方法
def check_number_exist(self):
"""
判断字符串中是否含有数字
"""
has_number = False
for c in self.password:
if c.isnumeric():
has_number = True
break
return has_number
def check_letter_exist(self):
"""
判断字符串中是否含有字母
"""
has_letter = False
for c in self.password:
if c.isalpha():
has_letter = True
break
return has_letter
def main():
"""
主函数
"""
try_times = 5
while try_times > 0:
password = input('请输入密码:')
# 实例化密码工具对象
password_tool = PasswordTool(password)
password_tool.process_password()
f = open('password_5.0.txt', 'a')
f.write('密码:{}, 强度:{}\n'.format(password, password_tool.strength_level))
f.close()
if password_tool.strength_level == 3:
print('恭喜!密码强度合格!')
break
else:
print('密码强度不合格!')
try_times -= 1
print()
if try_times <= 0:
print('尝试次数过多,密码设置失败!')
if __name__ == '__main__':
main()
测试结果:
请输入密码:dsadbhasd78a6d
恭喜!密码强度合格!
判断密码强弱6.0
'''
判断密码强弱6.0
'''
class PasswordTool:
"""
密码工具类
"""
def __init__(self, password):
# 类的属性
self.password = password
self.strength_level = 0
def process_password(self):
# 规则1:密码长度大于8
if len(self.password) >= 8:
self.strength_level += 1
else:
print('密码长度要求至少8位!')
# 规则2:包含数字
if self.check_number_exist():
self.strength_level += 1
else:
print('密码要求包含数字!')
# 规则3:包含字母
if self.check_letter_exist():
self.strength_level += 1
else:
print('密码要求包含字母!')
# 类的方法
def check_number_exist(self):
"""
判断字符串中是否含有数字
"""
has_number = False
for c in self.password:
if c.isnumeric():
has_number = True
break
return has_number
def check_letter_exist(self):
"""
判断字符串中是否含有字母
"""
has_letter = False
for c in self.password:
if c.isalpha():
has_letter = True
break
return has_letter
class FileTool:
"""
文件工具类
"""
def __init__(self, filepath):
self.filepath = filepath
def write_to_file(self, line):
f = open(self.filepath, 'a')
f.write(line)
f.close()
def read_from_file(self):
f = open(self.filepath, 'r')
lines = f.readlines()
f.close()
return lines
def main():
"""
主函数
"""
try_times = 5
filepath = 'password_6.0.txt'
# 实例化文件工具对象
file_tool = FileTool(filepath)
while try_times > 0:
password = input('请输入密码:')
# 实例化密码工具对象
password_tool = PasswordTool(password)
password_tool.process_password()
line = '密码:{}, 强度:{}\n'.format(password, password_tool.strength_level)
# 写操作
file_tool.write_to_file(line)
if password_tool.strength_level == 3:
print('恭喜!密码强度合格!')
break
else:
print('密码强度不合格!')
try_times -= 1
print()
if try_times <= 0:
print('尝试次数过多,密码设置失败!')
# 读操作
lines = file_tool.read_from_file()
print(lines)
if __name__ == '__main__':
main()
测试结果:
请输入密码:aa
密码长度要求至少8位!
密码要求包含数字!
密码强度不合格!
请输入密码:asdasd123
恭喜!密码强度合格!
['密码:aa, 强度:1\n', '密码:asdasd123, 强度:3\n']
您的资助是我最大的动力!
金额随意,欢迎来赏!