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()
View Code
1. 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()
View Code
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()
View Code
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()
View Code
1. 文件的操作
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()
View Code
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()
View Code
1. 面向对象的特点