'''
设计者:ISDF
版本:v1.0
日期:2019/4/3
'''
'''
设计者:ISDF
版本:v2.0
日期:2024/4/3
'''
class PasswordTool:
'''
密码工具类
'''
def __init__(self,password):
#类的属性
self.password = password
self.stregth_level = 0
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 process_passwod(self):
# 规则1:密码长度大于8
if len(self.password) >= 8:
self.stregth_level += 1
else:
print("密码长度必须大于8位!")
# 规则2:包含数字
if self.check_number_exist():
self.stregth_level += 1
else:
print("密码要求包含数字!")
# 规则2:包含字母
if self.check_letter_exist():
self.stregth_level += 1
else:
print("密码要求包含字母!")
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='paaword4.0.txt'
# 实例化对象
# 写文件
filetool = FileTool(filepath)
while try_times > 0:
password = input("请输入密码:")
tool = PasswordTool(password)
tool.process_passwod()
#密码强弱
stregth_level = tool.stregth_level
print()
# f = open('password_3.0.txt', 'a')
line = '密码:{},强度:{}\n'.format(password,stregth_level)
# f.close()
filetool.write_to_file(line)
if stregth_level==3:
print("恭喜密码合格")
break
else:
print("呵呵密码不合格")
try_times -= 1
if try_times <= 0:
print("密码输入次数太多,设置密码失败!!!!")
#读操作
lines = filetool.read_from_file()
print(lines)
if __name__ == '__main__':
main()
结果:
标签:stregth,level,python,self,密码,password,强弱,def From: https://blog.csdn.net/weixin_41583925/article/details/136748335