編寫代碼注重兩點能力的實現:
1、思維邏輯:思維邏輯有可以通過程序思維導圖輔助實現,同時,我們也可以像下面代碼中的新增功能注釋一樣,循序漸進的實現程序的功能;
2、細節完善:程序除了主要功能的實現外,添加一定的代碼注釋、功能說明、以及寫作信息也是必需的。
下面是一個用Python實現的判斷密碼強度的小程序:
其中涉及到了自定義的字符串處理函數:
def check_letter_exist(self):
"""
判斷字符串中是否有字母
:param password_str:
:return:
"""
has_letter = False
for d in self.password:
if d.isalpha():
has_letter = True
break
return has_letter
等,實現過程比較簡單。
""" 作者:虞曦非虞兮 功能:判斷密碼強度 版本:V6.1 日期:2018/12/18 2.0新增功能:限制密碼設置的次數;循環的終止 3.0新增功能:保存密碼和強度到文件中 3.1新增功能:將密碼強度由數字變成對應的中文解釋。 4.0新增功能:讀取文件中的密碼 5.0新增功能:將相關方法封裝成一個模塊,面向對象編程 6.0新增功能:定義一個文件工具類 6.1新增功能:需要密碼同時包含大小寫和特殊符號 """ class PasswordTool: """ 密碼工具類 """ # 實例化函數 def __init__(self, password): self.password = password self.strength_level = 0 # 類的方法 def process_password(self): # 規則1:密碼長度大於8 if len(self.password) > 8: self.strength_level += 1 else: print('密碼長度必須大於8位。') # 規則2:包含數字 if self.check_number_exist(): self.strength_level += 1 else: print('密碼必須包含數字') # 規則3:包含字母 if self.check_letter_exist(): self.strength_level += 1 else: print('密碼必須包含字母') # 規則4:包含大小寫字母 if self.check_upper_and_lower_exit(): self.strength_level += 1 else: print('密碼必須同時包含大小寫字母') # 規則5:包含特殊字母 if self.check_specal_symbol_exit(): self.strength_level += 1 else: print('密碼必須同時包含特殊字符') def check_number_exist(self): """ 判斷字符串中是否有數字 :param password_str: :return: """ has_number = False for c in self.password: if c.isnumeric(): has_number = True break return has_number def check_letter_exist(self): """ 判斷字符串中是否有字母 :param password_str: :return: """ has_letter = False for d in self.password: if d.isalpha(): has_letter = True break return has_letter def check_upper_and_lower_exit(self): """ 判斷是否同時有大小寫字母 :return: """ has_upper_and_lower = False has_upper = False has_lower = False for m in self.password: if m.isupper() == True: has_upper = True continue elif m.islower() == True: has_lower = True if has_upper == True: break else: continue else: continue if has_upper == True and has_lower == True: has_upper_and_lower = True return has_upper_and_lower def check_specal_symbol_exit(self): has_special_symbol_exit = False special_char_list = ['*', '#', '+', '!', '@', '$', '^', '%'] for i in range(len(special_char_list)): if self.password.find(special_char_list[i]): has_special_symbol_exit = True break return has_special_symbol_exit class FileTool: """ 文件工具類 """ def __init__(self, filepath): self.filepath = filepath def writ_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(): """ 主函數 :return: """ try_times = 5 file_path = 'password_6.0.txt' # 實例化文件對象 file_tool = FileTool(file_path) while try_times > 0: password = input('請輸入密碼:') # 實例化密碼工具對象 password_tool = PasswordTool(password) password_tool.process_password() # 寫文件操作 line = '密碼:{},強度:{}\n'.format(password, password_tool.strength_level) file_tool.writ_to_file(line) if password_tool.strength_level == 5: print('恭喜,密碼強度合格。') break else: print('密碼強度不合格。') try_times -= 1 print() if try_times <= 0: print('嘗試次數過多,密碼設置失敗。') # 讀操作 content = file_tool.read_from_file() print(content) if __name__ == '__main__': main()