PYTHON-字符编码&文件处理-练习

时间:2021-07-28 14:50:55
# 把多个账号密码写入文件中
# 编写登录功能,登录失败三次 程序退出。 # 登录账号密码定义,用列表分割,for循环取值=====判断对错=====循环条件3次退出
# with open(r'db.txt',mode='r') as f:
# db=f.read().split('|')
# # print(db)
# z=[]
# for x in db:
# y=x.split(':')
# # print(y)
# count=0
# z.append(y)
# print(z)
# while True:
# usr = input('username')
# pwd = input('password')
# if usr == z[0][0] and pwd == z[0][1]:
# print('login in')
# break
# else:
# print('error')
# count+=1
# if count>=3:
# print('blocked')
# break # for x in db:
# z = x.split(':')
# z.append(y)
# count = 0
# while True:
# usr = input('username')
# pwd = input('password')
# zz=[usr,pwd]
# if zz in y:
# print('login in')
# break
# else:
# print('error')
# count += 1
# if count >= 3:
# print('blocked')
# break # 1. 文件db2.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数
# apple 10 3
# tesla 100000 1
# mac 3000 2
# lenovo 30000 3
# chicken 10 3 ttl=0
count=0
with open(r'db2.txt',mode='r') as f:
db=f.read().split('\n')
# print(db)
for x in db:
y=x.split(' ')
# print(y)
ttl+=int(y[1])*int(y[2])
count+=1
if count < 5:
continue
print(ttl)