密码规则

长度至少8位,必须含有数字,字母
1
2
3
4
5
6
7

#长度判断:len()
#全是数字:isnumeric()
#全是字母:isalpha()
#全是小写:islower()
#全是大写:isupper()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def main():
password = input("请输入密码:")

level = 0

#规则1:密码长度至少8
if len(password) >= 8:
level+=1
else:
print("密码长度要求至少8位!")

#规则2:密码包含数字
hasNumber = 0

#规则3:密码包含字母
hasAlpha = 0

for i in password:
if i.isnumeric():
hasNumber+=1

if i.isalpha():
hasAlpha+=1

if hasNumber!=0 and hasAlpha!=0:
break

if hasNumber != 0:
level+=1
else:
print("密码必须包含数字!")

if hasAlpha != 0:
level+=1
else:
print("密码必须包含字母!")

print("密码强度 {}级!".format(level))

if __name__ == '__main__':
main()

循环终止

break 终止整个循环
continue 只终止本次循环,而不终止整个循环
1
2
3
4
5
6
7
8
9
def check_number(password):
has_number = False

for c in password:
if c.isnumeric():
has_number = True
break

return has_number
1
2
3
4
5
6
7
8
9
10
11
12
13
#增加次数限制
try_time = 0
while try_time > 0:
#主代码

if level == 3
break
else:
print("密码强度不合格!")
try_time -= 1

if try_time <= 0:
print("尝试次数过多,设置密码失败!")

文件基础

文件:存储在外部介质(硬盘,U盘)上的数据或信息的集合
文本文件:一般只有字符编码存储的文件 ,能够被最简单的文本编辑器直接读取
编码:信息从一种形式转换为另一种形式的过程
常见编码:ASCII, Unicode, UTF-8

文件操作

open(filename,mode)
mode: r   只读,文件不存在则报错
      w   只写,文件不存在则自动创建
      a   在文件末尾附加
      r+  读写

write()
writelines(string)

close()
1
2
3
4
5
#保存文件
if level == 3
f = open("d:\password.txt", "a")
f.write("密码:{}, 强度:{} \n".format(password, level))
f.close()
read()       读取整个文件内容,返回字符串
readline()   文件下一行内容,返回字符串
readlines()  返回整个文件内容的列表,每项是以换行符为结尾的一行字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#读取文件
f = onpe("d:\password.txt", "r")

content = ""

content = f.read()
print(content)

content = f.readline()
print(content)

content = f.readlines()
print(content)

for line in content:
print(line)

f.close()

类, 对象

密码工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class PassWordTool:
def __init__(self, password):
#类的属性
self.password = password
self.level = 0

#类的方法
def verify_password(self):
#规则1:密码长度至少8
if len(self.password) >= 8:
self.level+=1
else:
print("密码长度要求至少8位!")

#规则2:密码包含数字
if self.check_number():
self.level+=1
else:
print("密码必须包含数字!")

#规则3:密码包含字母
if self.check_letter():
self.level+=1
else:
print("密码必须包含字母!")

def check_number(self):
has_number = False
for c in self.password:
if c.isnumeric():
has_number = True
break
return has_number

def check_letter(self):
has_letter = False
for c in self.password:
if c.isalpha():
has_letter = True
break
return has_letter

#调用,实例化 PassWordTool
passwordTool = PassWordTool(password)
passwordTool.verify_password();
print("密码强度:{} ".format(passwordTool.level))

文件工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class FileTool:
def __init__(self, filepath):
self.filepath = filepath

def write_to_file(self, line):
f = open(self.filepath, 'a')
f.write(line)
f.close()

def read_from_file(self):
f = open(self.filepath, 'r')
lins = f.readlines()
f.close()
return lines

#调用,实例化 FileTool
fileTool = FileTool("d:\password.txt")
line = "密码:{}, 强度:{} \n".format(password, passwordTool.level)
fileTool.write_to_file(line)

lines = fileTool.read_from_file();
print("文本内容为:\n {}".format(lines))
面向对象的特点:
封装 -> 数据及相关操作打包,支持代码复用
继承 -> 子类借用父类的行为,避免重复操作 
        定义:class ClassName(SuperClassName)
多态 -> 在不同情况下用一个函数名启用不同方法,增强灵活性