'''
设计者:ISDF
版本:v1.0
日期:2019/4/3
''''''
设计者:ISDF
版本:v2.0
日期:2024/4/3
'''class PasswordTool:'''密码工具类'''def __init__(self,password):#类的属性self.password = passwordself.stregth_level = 0def check_number_exist(self):'''判断字符串是否有数字'''has_number = Falsefor c in self.password:if c.isnumeric():has_number = Truebreakreturn has_numberdef check_letter_exist(self):'''判断字符串是否有字母'''has_letter = Falsefor c in self.password:if c.isalpha():has_letter = Truebreakreturn has_letterdef process_passwod(self):# 规则1:密码长度大于8if len(self.password) >= 8:self.stregth_level += 1else:print("密码长度必须大于8位!")# 规则2:包含数字if self.check_number_exist():self.stregth_level += 1else:print("密码要求包含数字!")# 规则2:包含字母if self.check_letter_exist():self.stregth_level += 1else:print("密码要求包含字母!")class FileTool:'''文件工具类'''def __init__(self,filepath):self.filepath = filepathdef 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 = 5filepath='paaword4.0.txt'# 实例化对象# 写文件filetool = FileTool(filepath)while try_times > 0:password = input("请输入密码:")tool = PasswordTool(password)tool.process_passwod()#密码强弱stregth_level = tool.stregth_levelprint()# 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("恭喜密码合格")breakelse:print("呵呵密码不合格")try_times -= 1if try_times <= 0:print("密码输入次数太多,设置密码失败!!!!")#读操作lines = filetool.read_from_file()print(lines)if __name__ == '__main__':main()
结果: