class PWSTRDELL:
    def __init__(self, pw_str):#该方法在类实例化时会自动调用
        self.pw = pw_str
        self.strength_level = 0
        #通过初始化函数从外部传入
    def pw_str_process(self):
        #这里不用在传入password,直接用self.pw就可以了
        if len(self.pw)>=8:
            self.strength_level +=1
        else:
            print("密码至少8位")

        if self.pw_hasnumer():#虽然需要处理self.pw,但不必须要明显传入。因为def函数定义的时候参数是self
            self.strength_level +=1
        else:
            print("密码至少含1位数字")

        if self.pw_hasalpha():
            self.strength_level+=1
        else:
            print("密码至少含1位字母")
    def pw_hasnumer(self):
        has_number = False
        for i in self.pw:
            if i.isnumeric():
                has_number = True
                break
        return has_number
    def pw_hasalpha(self):
        has_alpha = False
        for i in self.pw:
            if i.isalpha():
                has_alpha = True
                break
        return has_alpha

class FILETOOL:
    def __init__(self, path, line):
        self.filepath = path
        self.line = line#"密码:{},强度:{},密码强度级别:{}\n".format(pw_str, pw_strong,pw_strong_word(pw_strong))

    def write_file(self):
        f = open(self.filepath, "a")
        f.write(self.line)
        f.close()
    def read_file(self):
        f = open(self.filepath,"r")
        lines= f.readlines()
        f.close()
        return lines
def main():
    x = 5
    path ="pwd_03.txt"
    line=""
    file_process = FILETOOL(path, line)

    while x > 0:
        pw_str = input("请输入密码-需包含至少8位,由字母数字组成的密码")
        #实例化class类的对象,传入参数,初始化类
        pw_tool = PWSTRDELL(pw_str)
        #类的初始化函数要求一个输入,所以这里需要输入一个函数
        #类初始化之后开始调用类的处理
        pw_tool.pw_str_process()

        print("输入密码{}的密码强度为{},".format(pw_str,pw_tool.strength_level))

        line = "输入密码{}的密码强度为{},".format(pw_str,pw_tool.strength_level)
        file_process.write_file()

        if pw_tool.strength_level>=3:
            print("恭喜,密码设置成功")
            break
        else:
            print("密码不符合要求,请重新设置\n")
        x -= 1

    else:
        print("尝试次数太多,请稍后再试")

    print(file_process.read_file())
if __name__=="__main__":
    main()

上面这段代码里,write的内容是空的,因为初始化的时候line写了是空字符串,虽然下面line重新赋值了,但是并没有改变类里line的值

上述line那行,需要改成

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
file_process.line = "输入密码{}的密码强度为{}\n".format(pw_str,pw_tool.strength_level) 这样才会写入值

或者用下面的方法:
class定义时去掉line,只在方法内引入line
class PWSTRDELL:
    def __init__(self, pw_str):#该方法在类实例化时会自动调用
        self.pw = pw_str
        self.strength_level = 0
        #通过初始化函数从外部传入
    def pw_str_process(self):
        #这里不用在传入password,直接用self.pw就可以了
        if len(self.pw)>=8:
            self.strength_level +=1
        else:
            print("密码至少8位")

        if self.pw_hasnumer():#虽然需要处理self.pw,但不必须要明显传入。因为def函数定义的时候参数是self
            self.strength_level +=1
        else:
            print("密码至少含1位数字")

        if self.pw_hasalpha():
            self.strength_level+=1
        else:
            print("密码至少含1位字母")
    def pw_hasnumer(self):
        has_number = False
        for i in self.pw:
            if i.isnumeric():
                has_number = True
                break
        return has_number
    def pw_hasalpha(self):
        has_alpha = False
        for i in self.pw:
            if i.isalpha():
                has_alpha = True
                break
        return has_alpha

class FILETOOL:
    def __init__(self, path):
        self.filepath = path
        #self.line = line#"密码:{},强度:{},密码强度级别:{}\n".format(pw_str, pw_strong,pw_strong_word(pw_strong))

    def write_file(self, line):#写入的line不是类的属性,而是这个方法需要写入line
        f = open(self.filepath, "a")
        f.write(line)
        f.close()
    def read_file(self):
        f = open(self.filepath,"r")
        lines= f.readlines()
        f.close()
        return lines
def main():
    x = 5
    path ="pwd_03.txt"
    line=""
    file_process = FILETOOL(path)

    while x > 0:
        pw_str = input("请输入密码-需包含至少8位,由字母数字组成的密码")
        #实例化class类的对象,传入参数,初始化类
        pw_tool = PWSTRDELL(pw_str)
        #类的初始化函数要求一个输入,所以这里需要输入一个函数
        #类初始化之后开始调用类的处理
        pw_tool.pw_str_process()

        print("输入密码{}的密码强度为{}\n".format(pw_str,pw_tool.strength_level))

        line = "输入密码{}的密码强度为{}\n".format(pw_str,pw_tool.strength_level)
        #file_process.line = "输入密码{}的密码强度为{}\n".format(pw_str,pw_tool.strength_level)
        file_process.write_file(line)

        if pw_tool.strength_level>=3:
            print("恭喜,密码设置成功")
            break
        else:
            print("密码不符合要求,请重新设置\n")
        x -= 1

    else:
        print("尝试次数太多,请稍后再试")

    print(file_process.read_file())
if __name__=="__main__":
    main()

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄