day 13 作业

import os
import time


# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
# def revise(path, txt, r_txt):
# with open(path, 'r+')as f:
# res = f.read()
# c_res = res.replace(txt, r_txt)
# f.seek(0)
# print(c_res)
# f.write(c_res)
#
#
# revise('/Users/yubin/Desktop/脱产14期/day13/a.txt', 'a', 'b')
# 2、编写tail工具
# with open('a.txt', 'r')as f:
# f.seek(0, 2)
# while True:
# res = f.read()
# if len(res) == 0:
# time.sleep(0.3)
# else:
# print(res)
# 3、编写登录功能
def login():
print('开始登录')
login_t = False
tag = False
count = 0
while count < 3:
in_name = input('输入名字:')
in_password = input('请输入密码:')
with open('db.txt', encoding='utf-8')as f, open('.db.txt.swap', mode='w', )as f1:
for i in f:
name, password, money, lock = i.strip('\n').split(':')
if in_name == name:
tag = True
print('用户存在,', end='')
if password == in_password and lock == 'lock_on':
print('账号被锁定,无法登陆')
count = 3
elif password == in_password and lock != 'lock_on':
print('登录成功')
count = 3
login_t = True
atm_name = in_name
else:
print('密码错误,三次后将被锁定')
count += 1
if count == 3:
print('三次错误被锁定')
lock = 'lock_on'
i = f'{name}:{password}:{money}:{lock}\n'
f1.write(i)
else:
if not tag:
print('用户不存在')
os.remove('db.txt')
os.rename('.db.txt.swap', 'db.txt')
return login_t, atm_name


# 4、编写注册功能
def register():
print('注册')
in_name = input('输入名字:')
in_password = input('请输入密码:')
with open('db.txt', mode='a', encoding='utf-8') as f1, open(r'db.txt', encoding='utf-8')as f2:
for i in f2:
a = i.strip('\n').split(':')
if in_name == a[0]:
print('已注册')
break
else:
f1.write(f'{in_name}:{in_password}:0:lock_off\n')
print('注册成功')


def out():
global tag
tag = False


def atm(user):
atm_f = {
1: take_money,
2: invest_money,
3: remove_money,
4: show_money
}
info = """
1 取钱
2 充钱
3 转钱
4 查询余额
"""
print(info)
chose = input('输入选择:')
if int(chose) in atm_f:
atm_f.get(int(chose))(user)
else:
print('输入不正确的选择')


def change_dbm(user,c_money):
with open('db.txt', mode='r', encoding='utf-8')as f,\
open('.db.txt.swap',mode='w')as f2:
for i in f:
a = i.strip('\n').split(':')
name,password,money,lock = a
if user == name:
money =int(money)
money=money+int(c_money)
i = f'{name}:{password}:{money}:{lock}\n'
f2.write(i)
os.remove('db.txt')
os.rename('.db.txt.swap', 'db.txt')
return money


def take_money(user):
c_money=input('输入要取的钱')
money=-int(c_money)
change_dbm(user,money)



def invest_money(user):
c_money=input('输入要存入的钱')
change_dbm(user,c_money)



def remove_money(user):
pass


def show_money(user):
print(change_dbm(user))


# 5、编写用户认证功能
res = {
out: 0,
login: 1,
register: 2
}
tag = True
while tag:
msg = """
0 退出
1 登录
2 注册
"""
print(msg)
cmd = input('请输入命令编号>>: ').strip()
for i in res:
if str(res.get(i)) == cmd:
info = i()
if_login, user = info
if if_login:
atm(user)

# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额

# 选做题中的选做题:登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作

猜你喜欢

转载自www.cnblogs.com/AaronY/p/12514847.html