python来判断密码强弱-类

'''
设计者: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()

结果:

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ISDF-工软未来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值