1,break,continue
相同点 | 不同点 | |
---|---|---|
break | 都是跳出循环的,用在while和for循环中 | 跳出整个循环;即循环条件没运行或者序列还没被递归完,也会停止执行循环语句。 |
continue | 跳出本次循环。跳过当前循环的剩余语句,然后继续进行下一轮循环 |
for x in range(100):
if x % 2 == 0:
break
print(x) #什么都不输出
for x in range(100):
if x % 2 == 0:
continue
print(x) #1,3,5,7,9...
2,文件操作
文本中涉及的概念简介:
文件 | 存储在外部介质中的数据或信息的集合 |
---|---|
文本文件 | 一般指只有字符编码存储的文件,能够被最简单的文本编辑器直接读取 |
编码 | 信息从一个形式转换为另一种形式的过程(ASCII,Unicode,UTF-8) |
文本操作步骤:
打开文件->操作文件->关闭文件
-
打开文件:建立文件与程序之间的关联
Open(filename,mode)
Filename:文件名(包括路径《只写文件名默认在程序目录下》),第一个参数是包含文件名的字符串。第二个参数是另一个字符串,其中包含一些描述文件 使用方式的字符。 模式可以’r’在文件才会被读出,‘w’ 写入方式(现有的文件具有相同的名称将被删除),并且 'a’打开了附加的文件; 写入文件的任何数据都会自动添加到最后。 'r+'打开文件进行读写。所述模式参数是可选的; 'r’如果省略则将被假定。
-
操作文件:写入,读取等:
(1)从计算机内存向文件写入数据Write(): 将文本数据写入文件中
Writelines():将字符串列表写入文件中(2)从计算机内存向文件写入数据
read():返回值为包含整个文件内容的一个字符串
readline():返回值为文件下一行内容的字符串(调用一次输出一行)
readlines():返回值为整个文件内容的列表,每项是以换行符为结尾的一行字符串 -
文件的遍历:
f = open("password.txt","r")
for line in f:
print("read:{}".format(line))
f.close()
f = open("password.txt","r")
for line in f.readlines():
print("read:{}".format(line))
f.close()
-
关闭文件:终止程序与文件的关联
Close()
调用 f.close()以关闭该文件并立即释放它使用的任何系统资源。如果您没有显式关闭文件,Python的垃圾收集器最终会破坏该对象并为您关闭打开的文件,但该文件可能会暂时保持打开状态。
更多内容可以参考Python官网:https://docs.python.org/3.7/tutorial/inputoutput.html?highlight=write file#reading-and-writing-files。或者 http://www.runoob.com/python/file-methods.html
3,类的定义
Class className
init(self)构造函数:初始化对象的各个属性,Self表示类的实例。
4,密码等级识别案例(Python)
#!/usr/bin/env python
#-*- coding:utf-8 -*-
'''
@author : FIGTHING
@file : pwd-strength1.0.py
@function: 判断输入密码的强弱
@software: Pycharm
@time : 2019/03/21/14:20
'''
#密码操作
class PasswordTool:
"""
密码工具类
"""
def __init__(self,password):
self.password = password
self.pwd_level = 0
def process_password(self):
# 判断密码长度是否大于8
if len(self.password) >= 8:
self.pwd_level += 1
else:
print("建议你的密码长度大于或等于8!!")
if self.check_number_exist():
self.pwd_level += 1
else:
print("建议您的密码包含数字!!")
if self.check_letter_exist():
self.pwd_level += 1
else:
print("建议您的密码包含字母!!")
#判断密码是否含有数字
def check_number_exist(self):
has_number = False
for c in self.password:
if c.isnumeric():
has_number =True
break
return has_number
#判断密码是否含有字母
def check_letter_exist(self):
has_letter = False
for c in self.password:
if c.isalpha():
has_letter =True
break
return has_letter
#读写文件
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_to_file(self):
f = open(self.filepath,"r")
lines = f.readlines()
f.close()
return lines
def main():
try_times = 5
filepath = "password.txt"
while try_times >= 0:
password = input("请输入您的密码")
#实例化类的对象
password_tool = PasswordTool(password)
password_tool.process_password()
#实例化类对象
file_tool = FileTool(filepath)
line = "密码:{},强度:{}\n".format(password,password_tool.pwd_level)
#写操作
file_tool.write_to_file(line)
if password_tool.pwd_level ==3:
print("---------您的智商和您的密码强度一样强")
break
else:
print("---------您的智商和您的密码强度一样弱")
try_times -= 1
print("您还可以尝试{}次".format(try_times))
if try_times <= 0:
print("************************")
print("您尝试的次数已经用完!!!")
#读操作
lines = file_tool.read_to_file()
print(lines)
if __name__ == "__main__":
main()
“”"***笔者的话:在实现比较大的项目时,文件操作也是经常用的,对于它的读写我们都得非常熟悉。更多内容大家可以参照上面的网址进行更深入的学习。FIGHTING...(热烈欢迎大家批评指正,互相讨论)
("Don’t let anyone tell you different." --《怪兽大学》
) ***"""