day12作业

# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改

# def modify(file, old, new):
# with open(r'%s'%file,'r',encoding='utf-8') as f:
# data = f.read()
# res = data.replace(old,new)
#
# with open(r'%s'%file,'w',encoding='utf-8') as f:
# f.write(res)
# print('修改成功!')
#
# f = input('请输入修改的文件路径:').strip()
# o = input('请输入要修改的内容:').strip()
# n = input('请输入修改后的内容:').strip()
# modify(f,o,n)

# 2、编写tail工具

# import time
#
# def tail():
# with open('login.txt', 'rb') as f:
# f.seek(0,2)
# while True:
# new = f.readline()
# if len(new) == 0:
# time.sleep(1)
# else:
# print(new.decode('utf-8'))
#
#
# tail()

# 3、编写登录功能

# def login(name, pwd):
# with open('db.txt', 'r', encoding='utf-8') as f:
# for line in f:
# user_name, user_pwd = line.strip('\n').split(':')
# if user_name == name and user_pwd == pwd:
# print('login successful')
# break
# else:
# print('name or pwd error')
#
# n = input('your name:').strip()
# p = input('your password:').strip()
# login(n, p)

# 4、编写注册功能

def register(name, pwd, money = 0):
with open('db.txt', 'a', encoding='utf-8') as f:
res = '%s:%s:%s\n'%(name, pwd, money)
f.write(res)
print('注册成功!')

n = input('your name:').strip()
p = input('your password:').strip()
register(n, p)

# 5、编写用户认证功能
# 选做题中的选做题:登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作
# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# u_name = None
# # def login(name, pwd):
# # with open('login.txt', 'r', encoding='utf-8') as f:
# # for line in f:
# # user_name, user_pwd = line.strip('\n').split(':')
# # if user_name == name and user_pwd == pwd:
# # print('login successful')
# # global u_name
# # u_name = name
# # break
# # else:
# # print('name or pwd error')
# #
# # n = input('your name:').strip()
# # p = input('your password:').strip()
# # login(n, p)
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改

def recharge(name, money):
with open('db.txt', 'r', encoding='utf-8') as f:
data = f.read()
user_name,user_pwd,user_money = data.strip('\n').split(':')
user_money = int(user_money)
if name == user_name:
money = money + user_money
user_money = str(user_money)
money = str(money)
res = data.replace(user_money, money)
with open('db.txt','w',encoding='utf-8') as f:
f.write(res)
print('充值成功!')

m = int(input('请输入充值金额:').strip())
recharge(u_name, m)

# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱

# def withdrawal(name,f_name, money):
# with open('db.txt', 'r', encoding='utf-8') as f:
# data = f.read()
# for line in data:
# user_name,user_pwd,user_money = data.strip('\n').split(':')
# user_money = int(user_money)
# if name == user_name:
# money = user_money - money
# user_money = str(user_money)
# money = str(money)
# res = data.replace(user_money, money)
# with open('db.txt','w',encoding='utf-8') as f:
# f.write(res)
# print('转账成功!')
#
# f_n = input('请输入转账对象:').strip()
# m = int(input('请输入转账金额:').strip())
# withdrawal(u_name, f_n, m)

# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# def withdrawal(name, money):
# with open('db.txt', 'r', encoding='utf-8') as f:
# data = f.read()
# for line in data:
# user_name,user_pwd,user_money = data.strip('\n').split(':')
# user_money = int(user_money)
# if name == user_name:
# money = user_money - money
# user_money = str(user_money)
# money = str(money)
# res = data.replace(user_money, money)
# with open('db.txt','w',encoding='utf-8') as f:
# f.write(res)
# print('提现成功!')
#
# m = int(input('请输入提现金额:').strip())
# withdrawal(u_name, m)

# 4、查询余额功能:输入账号查询余额
# def query(name):
# with open('db.txt', 'r', encoding='utf-8') as f:
# data = f.read()
# user_name, user_pwd, user_money = data.strip('\n').split(':')
# print(f'{user_name}的余额为:{user_money}')
#
#
#
# query(u_name)


posted @ 2020-07-20 18:30  画堂晨起  阅读(94)  评论(0编辑  收藏  举报