python作业6月12日

#今日内容
#1 数据类型剩余的内置方法
#2 字符编码
#3 文件处理
#4 函数基础
#列表里类型
list1=['tank','18','nan','guangdong']
#list1.insert(2,'oldboy')#插入函数 插入位置,插入内容
# 2为索引
#CTRL +斜杠呼出注释
#print(list1)
#print(list1.count('tank'))
#count 输出索引
#clear 清空
#list1.clear()
#print(list1)
# list2=list1.copy()
#不同于 =
#复制 直接赋值 完全复制
# list3=list1
# list1.append('666')
# print(list2)
# print(list3)
# =对后面有影响 是地址赋值
#以上是浅拷贝
#下面是深拷贝
# from copy import deepcopy
# list4=deepcopy(list1)
# list1.append('asd')
# print(list1)
# print(list4)
# 没有变化
# extend 合并
# list2=[1,2,3]
# list3=[4,5,6]
# list2.extend(list3)
# print(list2)
# reverse 反转
# list1.reverse()
# print(list1)
# list1.sort()
# print(list1)
# sort 默认升序
# 降序使用
# list1.sort(reverse=True)
# print(list1)
# 字典
# 1 按照key取存值
dict1={'name':'jjj','age':'18','sex':'fale'}
# print(dict1['name'])
# print(dict1.get('sex'))
# # 使用get不会出错
# print(dict.get('sex',female))
# 会输出默认值 因为key不存在
# 成员运算 in和not in
# print('name' in dict1)
# 删除 del
# del dict1["name"]
# print(dict1)
# pop ()出栈
# 不过多解释
# 随机取出字典中的popitem()
# keys 和values items
# print(dict1.keys())
# print(dict1.values())
# print(dict1.items())
# 循环
# for key in dict1:
# print(key)
# update()
# 更新 操作对象为另一个字典
# dict2={"work":"student"}
# dict1.update(dict2)
# tuple1=(1,2,3,4,5)
# 切片
# print(tuple1[0"6])
# 步长
# print(tuple1[0:6:2])
# 长度 直接LEN
# 成员运算 in和 not in
# 循环
# 不在赘述
# 集合类型
# 在大括号内
# 默认去重 数学性质
# set1={1,2,3,4,5,6,7,7}
# 文件基本读写
# open(参数1:文件名字,参数2:操作模式,参数3:指定字符编码)
# f:成为句柄
# f=open('open.txt',r)
# f.write('hello tank')
# res=f.read()
# f.close()
# 文件追加模式 a
# 操作模式
# 文件处理值上下文管理:with
# with会自带close()功能
# with open(r'/python/file.txt','w',encoding='utf-8') as f
# f.write('list is short')
# 读文件
# with open('666.jpg',,'wb') as f
# f.write(res.content)
# res=f.read()
# print(res)
# with open ('ship,mp4','rb') as f
# res=f.read()
# print(res)
# w.write(res)
# with open('tianyan_sys.mp4','rb')as f,open('tianyuan_sys_copy.mp4','wb')as w:
# # 函数部分
# # def register():
# # while True:
# # user=input('shuru').strip()
# # pwd=input('mima').strip()
# # re_pwd=input('querenmima').strip()
# # if pwd==re_pwd:
# # user_info=f'yonghuming:{user},mima{pwd}'
# # with open(f'{user}.txt','w',encoding='utf-8') as f:
# # f.write(user_info)
# # break
# # else:
# # print("budui!")
# # register()
# # 只会检测python代码 其他的不会放在检测器里面
# 作业 函数登陆功能
# 让用户输入用户名与密码
# 检验用户名是否存在
# 用户名存在后检验密码 成功打印 不成功重新打印
# 输错三次退出循环
下面是作业中的登陆部分
with open('file1.text','w',encoding='utf-8') as f:
f.write('用户名:jjj,密码:666666')

def login():
user = ''
pwd = ''
dict1 = {}
with open('file1.text','rt',encoding='utf-8')as w:
for line in w:
line = line.split('\n')[0].split(',')
for data in line:
if '用户名'in data:
user = data[4:]
else:
pwd = data[3:]
dict1[user] = pwd
while True:
user1 = input('请输入用户名:').strip()

if user1 in dict1 :
i = 1
while i <= 3:
pwd1 = input('请输入密码:').strip()
if pwd1 == dict1[user1]:
print('登陆成功!')
break
else:
i = i + 1
else:
print('密码错误超过三次!')
else:
print('用户不存在!')
break

login()
 





猜你喜欢

转载自www.cnblogs.com/jjjpython1/p/11013370.html