day11作业

"""
熬夜会变帅
"""
# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def alter(paths,content,new_content):
with open('../作业.txt', 'rt', encoding='utf-8') as f: # 读出要修改的文件
ret = f.read().replace(content,new_content) # 修改文件内容
with open(r'%s作业.txt'%paths,'wt',encoding='utf-8') as f: # 保存修改文件 并修改路径
f.write(ret)
alter(r'C:Users陈少DesktoppycharmPython新建文件夹','编写用户认证功能','')

# 2、编写tail工具

def too():

import time
with open(r'C:Users陈少DesktoppycharmPythonday10aalog.txt', 'a+b') as f:
f.seek(0, 2)
while True:
line = f.readline()
if len(line) == 0:
time.sleep(1)
else:
print(line.decode('utf-8'), end='')




# 3、编写登录功能
# name = input('>>>')
# pasd = input('>>>')
# if name == 'chen' and pasd == '123':
# print('登录成功!')

# 4、编写注册功能
# name= input('>>').strip()
# pasd= input('>>').strip()
# with open('login.txt','wt',encoding='utf-8') as f:
# f.write('%s:%s'%(name,pasd))


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

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

"""
db_A 为本程序的主数据库
db_B 为外数据库
"""
def login():
"""
登录, 本登录是直接验证, 不从文件中读取数据
:return: 无
"""
print('请先登录!')
name = input('账号')
pasd = input('密码')
if name == 'chen' and pasd == '123':
print('登录成功!')
atm()
else:
print('账号或密码错误')

def atm():
"""
整体结构
:return: 无返回值
"""
while True:
print("""
充值请按 (1)
转账请按 (2)
提现请按 (3)
查询余额请按 (4)
返回请按 (q)或(Q)
""")
cmd = input('>>')
if cmd == '1':
print('欢迎来到充值页面.')
top_up()
elif cmd == '2':
print('欢迎来到转账页面.')
transfer()
elif cmd == '3':
print('欢迎来到提现页面.')
TiXian()
elif cmd == '4':
print('欢迎来到查询页面.')
search()
elif cmd.upper() == 'Q':
break
else:
print('输入有误,请重新输入!')

def top_up():
"""
充值, 充值成功后写入文件 db.txt
:return: 无返回值
"""
tag = True
while tag:
money = input('请输入充值金额:').strip()
if not money.isdigit(): # 输入金额只能为数字
print('输入有误,请重新输入!')
continue
else:
with open('db_A.txt','rt',encoding='utf-8') as f:
ret = f.read()
sum_money = int(ret) + int(money)
sum_money = str(sum_money)
with open('db_A.txt','wt',encoding='utf-8')as f:
f.write(sum_money)
print('充值成功')
cmd = input('继续充值请按 (1), 其他键退出:').strip()
if cmd.upper() == "1": # 判断是否继续充值
top_up()
else:
tag = False

def transfer():
"""
转账功能, 转出数额从db_A中减
此处db_B用户是指定的, 没有设置输入用户判断
:return: 无返回值
"""
tag = True
while tag:
money = input('请输入转账金额:').strip()
if not money.isdigit(): # 输入金额只能为数字
print('输入有误,请重新输入!')
continue
else:
with open('db_A.txt', 'rt', encoding='utf-8') as f:
ret = f.read()
sum_money_A = int(ret) - int(money)
sum_money_A = str(sum_money_A)
with open('db_A.txt', 'wt', encoding='utf-8')as f:
f.write(sum_money_A)
with open('db_B.txt', 'rt', encoding='utf-8') as f:
res = f.read()
sum_money_B = int(res) + int(money)
sum_money_B = str(sum_money_B)
with open('db_B.txt','wt',encoding='utf-8') as f: # 将金额写入B账户
f.write(sum_money_B)
print('转账成功!')
cmd = input('继续转账请按 (1), 其他键退出:').strip()
if cmd == "1": # 判断是否继续提现
TiXian()
else:
tag = False


def TiXian():
"""
提现功能 不指定提现账户
:return: 无返回值
"""
tag = True
while tag:
TiXian1 = input('请输入提现金额:').strip()
if not TiXian1.isdigit(): # 输入金额只能为数字
print('输入有误,请重新输入!')
continue
with open('db_A.txt', 'rt', encoding='utf-8') as f:
ret = f.read()
sum_money_A = int(ret) - int(TiXian1)
sum_money_A = str(sum_money_A)
with open('db_A.txt', 'wt', encoding='utf-8')as f:
f.write(sum_money_A)
print('提现成功!')
cmd = input('继续提现请按 (1), 其他键退出:').strip()
if cmd == "1": # 判断是否继续提现
TiXian()
else:
tag = False

def search():
"""
查询 A 用户金额
:return: 无返回值
"""
with open('db_A.txt','rt',errors='utf-8') as f:
print(f.read())
N = input('enter exit')
return N

while True:
login()
 
原文地址:https://www.cnblogs.com/Knge/p/13137987.html