1 day作业
2 with open(r'file1.text','w',encoding='utf-8') as f:
3 f.write('用户名:xm,密码:108595.\n用户名:xm1998,密码:111008.')
4
5 def login():
6 user = ''
7 pwd = ''
8 dict1 = {}
9 with open('file1.text','rt',encoding='utf-8')as w:
10 for line in w:
11 line = line.split('\n')[0].split(',')
12 for data in line:
13 if '用户名'in data:
14 user = data[4:]
15 else:
16 pwd = data[3:]
17 dict1[user] = pwd
18 while True:
19 user1 = input('请输入用户名:').strip()
20
21 if user1 in dict1 :
22 i = 1
23 while i <&#61; 3:
24 pwd1 &#61; input(&#39;请输入密码&#xff1a;&#39;).strip()
25 if pwd1 &#61;&#61; dict1[user1]:
26 print(&#39;登陆成功&#xff01;&#39;)
27 break
28 else:
29 i &#61; i &#43; 1
30 else:
31 print(&#39;密码错误超过三次&#xff01;&#39;)
32 else:
33 print(&#39;用户不存在&#xff01;&#39;)
34 break
35
36 login()
运行结果
请输入用户名&#xff1a;xm1998
请输入密码&#xff1a;111008
请输入密码&#xff1a;111111
请输入密码&#xff1a;111260
密码错误超过三次&#xff01;
上课笔记
1 # 列表类型:
2 # 1.insert&#xff08;&#xff09; #插入
3 # 第一个参数:索引 第二个参数&#xff1a;插入的值
4 list1 &#61; [&#39;tank&#39;, 18, &#39;male&#39;, 3.0, 9, &#39;广东&#39;, [1, 2]]
5
6 list1.insert(2,&#39;oldboy&#39;)
7 print(list1)
8
9 # 2.pop() 取出
10 # 3.remove() 移除
11
12 # 4.count() 查看某个值得个数
13 print(list1.count(&#39;tank&#39;))
14
15 # 5.index() 查看值的索引
16 print(list1.index(&#39;广东&#39;),&#39;___广东&#39;)
17
18 # 6.clear() 清空列表的值
19 list1.clear()
20 print(list1)
21
22 # 7.copy() 浅拷贝
23 # 将list1的内存地址浅拷贝赋值给list2
24 list2 &#61; list1.copy()
25 print(list2,&#39;添加值前&#39;)
26
27 # 将list1的原地址直接赋值给list3
28 list3 &#61; list1
29 print(list3,&#39;添加值前&#39;)
30
31 # 深拷贝&#xff08;&#xff09;
32 from copy import deepcopy
33 # 将list1的值深拷贝赋值给list4
34 list4 &#61; deepcopy(list1)
35
36 # 追加jason到list1中国
37 list1.append(&#39;jason&#39;)
38 print(list2, &#39;添加值后&#39;)
39 print(list3, &#39;添加值后&#39;)
40
41 # 给list1中的可变列表进行追加值
42 list1[8].append(&#39;tank&#39;)
43
44 # 打印直接赋值&#xff0c;深&#xff0c;浅拷贝的结果
45 # 浅拷贝&#xff1a;list1的列表中外层值改变对其不影响
46 # 但对list1中的可变类型进行修改则会随之改变值
47 print(list2)
48 print(list3)
49
50 # 深拷贝:把list1中的所有值完全拷贝到一个新的地址中
51 # 进而与list1完全隔离开
52 print(list4)
53
54 # 8.extend() # 合并
55 list1 &#61; [1, 2, 3]
56 list2 &#61; [4, 5, 6]
57 list1.extend(list2)
58 print(list1)
59
60 # 9.reserve() # 反转
61 list1.reverse()
62 print(list1)
63
64 # 10.sort() # 排序
65 list3 &#61; [1, 35, 34, 6, 8, 3]
66 # 升序
67 list3.sort()
68 print(list3)
69 # 降序
70 list3.sort(reverse&#61;True)
71 print(list3)
72
73 # 字典的常用方法
74 # 按照key取值/存值
75 dict1 &#61; {&#39;name&#39;: &#39;徐盟&#39;, &#39;age&#39;: 20, &#39;gender&#39;: &#39;man&#39;, &#39;school&#39;: &#39;安徽工程大学&#39;}
76
77 # 根据key取徐盟的学校
78 print(dict1[&#39;school&#39;])
79 print(dict1[&#39;age&#39;])
80
81 # 1.get()
82 # 第一个参数是字典的key
83 # 第二个参数是默认值&#xff0c;若key存在则取key对应的值&#xff0c;否则取默认值
84 # print(dict1.get(&#39;school&#39;, &#39;安徽工程大学&#39;))
85 print(dict1.get(&#39;age&#39;, 20))
86
87 # 2.len()
88 print(len(dict1))
89
90 # 3.成员运算in和not in
91 print(&#39;name&#39;in dict1)
92 print(&#39;sal&#39;in dict1)
93 print(&#39;sal&#39;not in dict1)
94
95 # 4.删除
96 del dict1["name"]
97 print(dict1)
98
99 # pop()
100 # 根据字典中的key取出对应的值赋值给变量name
101 name &#61; dict1.pop(&#39;name&#39;)
102 print(dict1)
103 print(name)
104
105 # 随机取出字典中的某个值
106 dict.popitem()
107 print(dict1)
108
109 # 5.key,value items
110 print(dict1.keys())
111 print(dict1.value())
112 print(dict1.items())
113
114 # 6.循环
115 # 循环字典中所有的key
116 for key in dict1:
117 print(key)
118
119 # 7.update()
120 print(dict1)
121 dict2 &#61; {"work": "student"}
122 # 把dict2加到dict1字典中
123 dict1.update(dict2)
124 print(dict1)
125
126 # 元组类型&#xff08;在小括号内&#xff0c;以逗号隔开存放多个值&#xff09;
127 # 注意&#xff1a;元组与列表的区别&#xff0c;元组是不可变类型&#xff0c;列表是可变类型
128 tuple1 &#61; (1, 2, 3, 4, 5, 6)
129 print(tuple1)
130 # 优先掌握
131 # 1.按索引取值
132 print(tuple1[2])
133
134 # 2.切片&#xff08;顾头不顾尾&#xff09;
135 print(tuple1[0:6])
136 # 步长
137 print(tuple1[0:6:2])
138 # 3.成员运算 in 和not in
139 print(1 in tuple1)
140 print(1 not in tuple1)
141 # 4.长度
142 print(len(tuple1))
143
144 # 5.循环
145 for line in tuple1:
146 print(line)
147
148
149 # 三 集合类型&#xff08;一般存放去重&#xff09;
150 # 在{}以逗号隔开&#xff0c;可存放多个值&#xff0c;但集合会自带默认去重功能。
151 set1 &#61; {1, 2, 3, 4, 2, 1, 3, 4}
152 print(set1)
153
154 # 集合是无需的
155 set1 &#61; set()
156 set2 &#61; {}
157 print(set1)
158 print(set2)
159
160 set2[&#39;name&#39;] &#61; &#39;tank&#39;
161 print(type(set2))
162
163 # 文件读写的基本使用
164
165 # 对文本进行操作
166 # open&#xff08;参数1&#xff1a;文件的绝对路径/文件的名字&#xff0c; 参数2: 操作模式&#xff0c; 参数3&#xff1a;指定字符编码&#xff09;
167 # f&#xff1a;称之为句柄
168 # r&#xff1a;避免转义符
169
170 # 打开文件会产生两种资源&#xff0c;一种是python解释器与python文件的资源,程序结束python会自动回收
171 # 另一种是操作系统打开文件的资源&#xff0c;文件打开后&#xff0c;操作系统并不会帮我们自动回收&#xff0c;所以需要手动回收
172
173 # 写文件
174 f &#61; open(
175 r&#39;C:\Users\administortra\PycharmProjects\untitled\xs.txt&#39;,
176 mode&#61;"wt",
177 encoding&#61;&#39;utf-8&#39;)
178 f.write(&#39;hello,world&#39;)
179 f.close()
180
181 f &#61; open(r&#39;C:\Users\administortra\PycharmProjects\untitled\xs.txt&#39;,
182 &#39;r&#39;,
183 encoding&#61;&#39;utf-8&#39;)
184 res &#61; f.read()
185 print(res)
186
187 f &#61; open(r&#39;C:\Users\administortra\PycharmProjects\untitled\xs.txt&#39;,
188 &#39;a&#39;,
189 encoding&#61;&#39;utf-8&#39;)
190 f.write(&#39; hello jason&#39;)
191 f.close()
192
193 # 文件处理之上下文管理&#xff1a;with
194 # with会自带close()功能&#xff0c;
195 # 会在文件处理完以后自动调用close&#xff08;&#xff09;关闭文件
196
197 # 写文件
198 with open(r&#39;file1.text&#39;,&#39;w&#39;,encoding&#61;&#39;utf-8&#39;) as f:
199 f.write(&#39;life is short,you need python!&#39;)
200
201 # 读文件
202 with open(r&#39;file1.text&#39;,&#39;r&#39;,encoding&#61;&#39;utf-8&#39;) as f:
203 res &#61; f.read()
204 print(res)
205
206 # 文件追加
207 with open(r&#39;file1.text&#39;,&#39;a&#39;,encoding&#61;&#39;utf-8&#39;) as f:
208 f.write(&#39;\n AAA&#39;)
209
210 import requests
211 res &#61; requests.get(
212 &#39;某网站.jpg&#39;)
213
214 # 写入图片
215 with open(&#39;大神.jpg&#39;, &#39;wb&#39;) as f:
216 f.write(res.content)
217
218 # 读取图片
219 with open(&#39;大神.jpg&#39;,&#39;rb&#39;) as f:
220 res &#61; f.read()
221 print(res)
222
223 # 文件拷贝操作
224 with open(&#39;大神.jpg&#39;, &#39;rb&#39;) as f,open(&#39;小鬼.jpg&#39;,&#39;wb&#39;) as w:
225 res &#61; f.read()
226 w.write(res)
227
228 # 读写视频
229 with open(&#39;tianyan_sys.mp4&#39;, &#39;rb&#39;,) as f, open(&#39;tianyan_sys_copy.mp4&#39;, &#39;wb&#39;) as w:
230 res &#61; f.read()
231 print(res)
232 w.write(res)
233
234 # 一行一行读文件
235 with open(&#39;tianyan_sys.mp4&#39;, &#39;rb&#39;,) as f, open(&#39;tianyan_sys_copy.mp4&#39;, &#39;wb&#39;) as w:
236 # 一次打开文件所有内容&#xff0c;若文件的大小超出内存的大小会导致内存溢出
237 f.read()
238 # 一行一行读取文件内容&#xff0c;一行一行写入文件中,避免内存溢出
239 for line f:
240 w.write(line)
241 # res &#61; f.read()
242 # print(res)
243 # w.write(res)
244
245 # 1.什么是函数&#xff1f;
246 # 函数相当于工具&#xff0c;需要事先准备好&#xff0c;在需要用时再使用
247
248 # 2.如何使用函数&#xff1f;
249 # 函数必须先定义&#xff0c;后调用
250
251 # 3.函数的语法&#xff1a;
252 # def 函数名&#xff08;参数1&#xff0c;参数2...&#xff09;&#xff1a;
253 # 注释
254 # 函数的说明
255 # 水杯&#xff0c;用来盛水和喝水
256 # .....
257 # 函数体代码&#xff08;逻辑代码&#xff09;
258 # return 返回值
259
260 # 注册功能
261 def register():
262 &#39;&#39;&#39;
263 此函数注册功能&#xff1a;
264 :return
265 &#39;&#39;&#39;
266 while True:
267 # 让用户输入用户名
268 user &#61; input(&#39;请用户输入用户名&#xff1a;&#39;).strip()
269 pwd &#61; input(&#39;请输入密码&#xff1a;&#39;).strip()
270 re_pwd &#61; input(&#39;请确认密码&#xff1a;&#39;).strip()
271
272 # 判断两次输入的密码是否一致
273 if pwd &#61;&#61; re_pwd:
274 # 格式化字符串的三种方式&#xff1a;
275 # user_info &#61; &#39;用户名&#xff1a;%s&#xff0c;密码&#xff1a;%s&#39; % (user,pwd)
276 # user_info &#61; &#39;用户名&#xff1a;{}&#xff0c;密码&#xff1a;{}&#39;.format(user,pwd)
277 # 字符串前写一个相当于调用format方法
278 user_info &#61; f&#39;用户名:%s,密码:%s&#39; % (user, pwd)
279 # 把用户信息写入文件中
280 with open(&#39;user.txt&#39;, &#39;w&#39;, encoding&#61;&#39;utf-8&#39;) as f:
281 f.write(user_info)
282 break
283 else:
284 print(&#39;两次密码不一致&#xff0c;请重新输入&#xff01;&#39;)
285 # 调用函数&#xff1a;函数名&#43;括号 即调用函数。