日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

class类初始化之后调用赋值问题记录

發布時間:2025/3/15 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 class类初始化之后调用赋值问题记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class PWSTRDELL:def __init__(self, pw_str):#該方法在類實例化時會自動調用self.pw = pw_strself.strength_level = 0#通過初始化函數從外部傳入def pw_str_process(self):#這里不用在傳入password,直接用self.pw就可以了if len(self.pw)>=8:self.strength_level +=1else:print("密碼至少8位")if self.pw_hasnumer():#雖然需要處理self.pw,但不必須要明顯傳入。因為def函數定義的時候參數是selfself.strength_level +=1else:print("密碼至少含1位數字")if self.pw_hasalpha():self.strength_level+=1else:print("密碼至少含1位字母")def pw_hasnumer(self):has_number = Falsefor i in self.pw:if i.isnumeric():has_number = Truebreakreturn has_numberdef pw_hasalpha(self):has_alpha = Falsefor i in self.pw:if i.isalpha():has_alpha = Truebreakreturn has_alphaclass FILETOOL:def __init__(self, path, line):self.filepath = pathself.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 = 5path ="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("恭喜,密碼設置成功")breakelse:print("密碼不符合要求,請重新設置\n")x -= 1else:print("嘗試次數太多,請稍后再試")print(file_process.read_file()) if __name__=="__main__":main()

上面這段代碼里,write的內容是空的,因為初始化的時候line寫了是空字符串,雖然下面line重新賦值了,但是并沒有改變類里line的值

上述line那行,需要改成

file_process.line = "輸入密碼{}的密碼強度為{}\n".format(pw_str,pw_tool.strength_level) 這樣才會寫入值

或者用下面的方法:
class定義時去掉line,只在方法內引入line class PWSTRDELL:def __init__(self, pw_str):#該方法在類實例化時會自動調用self.pw = pw_strself.strength_level = 0#通過初始化函數從外部傳入def pw_str_process(self):#這里不用在傳入password,直接用self.pw就可以了if len(self.pw)>=8:self.strength_level +=1else:print("密碼至少8位")if self.pw_hasnumer():#雖然需要處理self.pw,但不必須要明顯傳入。因為def函數定義的時候參數是selfself.strength_level +=1else:print("密碼至少含1位數字")if self.pw_hasalpha():self.strength_level+=1else:print("密碼至少含1位字母")def pw_hasnumer(self):has_number = Falsefor i in self.pw:if i.isnumeric():has_number = Truebreakreturn has_numberdef pw_hasalpha(self):has_alpha = Falsefor i in self.pw:if i.isalpha():has_alpha = Truebreakreturn has_alphaclass 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不是類的屬性,而是這個方法需要寫入linef = 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 = 5path ="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("恭喜,密碼設置成功")breakelse:print("密碼不符合要求,請重新設置\n")x -= 1else:print("嘗試次數太多,請稍后再試")print(file_process.read_file()) if __name__=="__main__":main()

?

轉載于:https://www.cnblogs.com/qiu-1010/p/10672183.html

總結

以上是生活随笔為你收集整理的class类初始化之后调用赋值问题记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。