1 """ 2 做者:王鑫正 3 版本:1.0 4 日期:2018/9/26 5 功能:判斷密碼強弱 6 """ 7 8 9 def check_number_exist(password_str): 10 """ 11 判斷字符串中是否含有數字 12 """ 13 for c in password_str: 14 if c.isnumeric(): 15 return True 16 return False 17 18 19 def check_letter_exist(password_str): 20 """ 21 判斷字符串中是否含有字母 22 """ 23 for c in password_str: 24 if c.isalpha(): 25 return True 26 return False 27 28 29 def main(): 30 """ 31 主函數 32 """ 33 password = input('請輸入密碼:') 34 35 # 密碼強度 36 strength_level = 0 37 38 # 規則1:密碼長度大於8 39 if len(password) >= 8: 40 strength_level += 1 41 else: 42 print('密碼長度要求至少8位') 43 44 # 規則2:密碼長達包含數字 45 if check_number_exist(password): 46 strength_level += 1 47 else: 48 print('密碼要求包含數字') 49 50 # 規則3:密碼長達包含字母 51 if check_letter_exist(password): 52 strength_level += 1 53 else: 54 print('密碼要求包含字母') 55 56 if strength_level == 3: 57 print('恭喜!密碼強度合格') 58 else: 59 print('密碼強度不合格') 60 61 62 if __name__ == '__main__': 63 main()
1. Python字符串python
1 """ 2 做者:王鑫正 3 版本:2.0 4 日期:2018/9/26 5 功能:判斷密碼強弱 6 2.0增長功能:限制密碼設置次數;循環的終止 7 """ 8 9 10 def check_number_exist(password_str): 11 """ 12 判斷字符串中是否含有數字 13 """ 14 has_number = False 15 16 for c in password_str: 17 if c.isnumeric(): 18 has_number = True 19 break 20 21 return has_number 22 23 24 def check_letter_exist(password_str): 25 """ 26 判斷字符串中是否含有字母 27 """ 28 has_letter = False 29 30 for c in password_str: 31 if c.isalpha(): 32 has_letter = True 33 break 34 35 return has_letter 36 37 38 def main(): 39 """ 40 主函數 41 """ 42 try_timer = 5 43 44 while try_timer > 0: 45 password = input('請輸入密碼:') 46 47 # 密碼強度 48 strength_level = 0 49 50 # 規則1:密碼長度大於8 51 if len(password) >= 8: 52 strength_level += 1 53 else: 54 print('密碼長度要求至少8位') 55 56 # 規則2:密碼長達包含數字 57 if check_number_exist(password): 58 strength_level += 1 59 else: 60 print('密碼要求包含數字') 61 62 # 規則3:密碼長達包含字母 63 if check_letter_exist(password): 64 strength_level += 1 65 else: 66 print('密碼要求包含字母') 67 68 if strength_level == 3: 69 print('恭喜!密碼強度合格') 70 break 71 else: 72 print('密碼強度不合格') 73 try_timer -= 1 74 75 print() 76 77 if try_timer <= 0: 78 print('嘗試次數過多,密碼設置失敗!') 79 80 81 if __name__ == '__main__': 82 main()
1. 循環的終止編程
1 """ 2 做者:王鑫正 3 版本:3.0 4 日期:2018/9/26 5 功能:判斷密碼強弱 6 3.0增長功能: 保存設置的密碼及其對用的強度到文件中 7 """ 8 9 10 def check_number_exist(password_str): 11 """ 12 判斷字符串中是否含有數字 13 """ 14 has_number = False 15 16 for c in password_str: 17 if c.isnumeric(): 18 has_number = True 19 break 20 21 return has_number 22 23 24 def check_letter_exist(password_str): 25 """ 26 判斷字符串中是否含有字母 27 """ 28 has_letter = False 29 30 for c in password_str: 31 if c.isalpha(): 32 has_letter = True 33 break 34 35 return has_letter 36 37 38 def main(): 39 """ 40 主函數 41 """ 42 try_timer = 5 43 44 while try_timer > 0: 45 password = input('請輸入密碼:') 46 47 # 密碼強度 48 strength_level = 0 49 50 # 規則1:密碼長度大於8 51 if len(password) >= 8: 52 strength_level += 1 53 else: 54 print('密碼長度要求至少8位') 55 56 # 規則2:密碼長達包含數字 57 if check_number_exist(password): 58 strength_level += 1 59 else: 60 print('密碼要求包含數字') 61 62 # 規則3:密碼長達包含字母 63 if check_letter_exist(password): 64 strength_level += 1 65 else: 66 print('密碼要求包含字母') 67 68 f = open('password_3.0.txt', 'a') 69 f.write('密碼:{},強度:{}\n'.format(password, strength_level)) 70 f.close() 71 72 if strength_level == 3: 73 print('恭喜!密碼強度合格') 74 break 75 else: 76 print('密碼強度不合格') 77 try_timer -= 1 78 79 print() 80 81 if try_timer <= 0: 82 print('嘗試次數過多,密碼設置失敗!') 83 84 85 if __name__ == '__main__': 86 main()
1. 文件的基礎安全
2. 文件的操做編輯器
1 def main(): 2 """ 3 主函數 4 """ 5 6 # 讀取文件 7 f = open('password_3.0.txt', 'r') 8 9 # 1. read() 10 content = f.read() 11 print(content) 12 13 # 2. readline() 14 line = f.readline() 15 print(line) 16 17 # 3. readlines() 18 for line in f: 19 print('read:{}'.format(line)) 20 21 for line in f.readlines(): 22 print('read:{}'.format(line)) 23 24 f.close() 25 26 27 if __name__ == '__main__': 28 main()
1. 文件的操做ide
2. 文件的遍歷:函數
1 for line in f: 2 print('read:{}'.format(line)) 3 4 for line in f.readlines(): 5 print('read:{}'.format(line))
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # author: Kevin.Wang 4 # time : 2018/9/26 5 6 7 """ 8 做者:王鑫正 9 版本:5.0 10 日期:2018/9/26 11 功能:判斷密碼強弱 12 5.0增長功能:定義一個password工具類 13 """ 14 15 16 class PasswordTool: 17 """ 18 密碼工具類 19 """ 20 def __init__(self, password): 21 # 類的屬性 22 self.password = password 23 self.strength_level = 0 24 25 # 類的方法 26 def process_password(self): 27 # 規則1:密碼長度大於8 28 if len(self.password) >= 8: 29 self.strength_level += 1 30 else: 31 print('密碼長度要求至少8位') 32 33 # 規則2:密碼長達包含數字 34 if self.check_number_exist(): 35 self.strength_level += 1 36 else: 37 print('密碼要求包含數字') 38 39 # 規則3:密碼長達包含字母 40 if self.check_letter_exist(): 41 self.strength_level += 1 42 else: 43 print('密碼要求包含字母') 44 45 def check_number_exist(self): 46 """ 47 判斷字符串中是否含有數字 48 """ 49 has_number = False 50 51 for c in self.password: 52 if c.isnumeric(): 53 has_number = True 54 break 55 56 return has_number 57 58 def check_letter_exist(self): 59 """ 60 判斷字符串中是否含有字母 61 """ 62 has_letter = False 63 64 for c in self.password: 65 if c.isalpha(): 66 has_letter = True 67 break 68 69 return has_letter 70 71 72 def main(): 73 """ 74 主函數 75 """ 76 try_timer = 5 77 78 while try_timer > 0: 79 password = input('請輸入密碼:') 80 81 # 實例化密碼工具對象 82 password_tool = PasswordTool(password) 83 password_tool.process_password() 84 85 f = open('password_5.0.txt', 'a') 86 f.write('密碼:{},強度:{}\n'.format(password, password_tool.strength_level)) 87 f.close() 88 89 if password_tool.strength_level == 3: 90 print('恭喜!密碼強度合格') 91 break 92 else: 93 print('密碼強度不合格') 94 try_timer -= 1 95 96 print() 97 98 if try_timer <= 0: 99 print('嘗試次數過多,密碼設置失敗!') 100 101 102 if __name__ == '__main__': 103 main()
1. 面向過程 vs 面向對象工具
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # author: Kevin.Wang 4 # time : 2018/9/26 5 6 7 """ 8 做者:王鑫正 9 版本:6.0 10 日期:2018/9/26 11 功能:判斷密碼強弱 12 6.0增長功能:將文件操做封裝到一個類中 13 """ 14 15 16 class FileTools: 17 """ 18 文件工具類 19 """ 20 def __init__(self,filepath): 21 self.filepath = filepath 22 23 def write_to_file(self, line): 24 f = open(self.filepath, 'a') 25 f.write(line) 26 f.close() 27 28 def read_form_file(self): 29 f = open(self.filepath, 'r') 30 lines = f.readlines() 31 f.close() 32 return lines 33 34 35 class PasswordTool: 36 """ 37 密碼工具類 38 """ 39 def __init__(self, password): 40 # 類的屬性 41 self.password = password 42 self.strength_level = 0 43 44 # 類的方法 45 def process_password(self): 46 # 規則1:密碼長度大於8 47 if len(self.password) >= 8: 48 self.strength_level += 1 49 else: 50 print('密碼長度要求至少8位') 51 52 # 規則2:密碼長達包含數字 53 if self.check_number_exist(): 54 self.strength_level += 1 55 else: 56 print('密碼要求包含數字') 57 58 # 規則3:密碼長達包含字母 59 if self.check_letter_exist(): 60 self.strength_level += 1 61 else: 62 print('密碼要求包含字母') 63 64 def check_number_exist(self): 65 """ 66 判斷字符串中是否含有數字 67 """ 68 has_number = False 69 70 for c in self.password: 71 if c.isnumeric(): 72 has_number = True 73 break 74 75 return has_number 76 77 def check_letter_exist(self): 78 """ 79 判斷字符串中是否含有字母 80 """ 81 has_letter = False 82 83 for c in self.password: 84 if c.isalpha(): 85 has_letter = True 86 break 87 88 return has_letter 89 90 91 def main(): 92 """ 93 主函數 94 """ 95 try_timer = 5 96 file_path = 'password_6.0.txt' 97 # 實例化文件工具對象 98 file_tool = FileTools(file_path) 99 100 while try_timer > 0: 101 password = input('請輸入密碼:') 102 103 # 實例化密碼工具對象 104 password_tool = PasswordTool(password) 105 password_tool.process_password() 106 107 line = '密碼:{},強度:{}\n'.format(password, password_tool.strength_level) 108 # 寫文件 109 file_tool.write_to_file(line) 110 111 if password_tool.strength_level == 3: 112 print('恭喜!密碼強度合格') 113 break 114 else: 115 print('密碼強度不合格') 116 try_timer -= 1 117 118 print() 119 120 if try_timer <= 0: 121 print('嘗試次數過多,密碼設置失敗!') 122 123 # 讀操做 124 lines = file_tool.read_form_file() 125 print(lines) 126 127 128 if __name__ == '__main__': 129 main()
1. 面向對象的特色編碼