六月十五号作业

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

def a(add,o,n):
import os
with open(r'%s'%add, mode='rt', encoding='utf-8') as read_f,\
open(r".%s"%add,mode='wt',encoding='utf-8') as write_f:
for line in read_f:
write_f.write(line.replace(o,n))

os.remove(r'%s'%add)
os.rename(r'.%s'%add,r'%s'%add)

add = input('修改的文件路径:').strip()
o = input('要修改的内容:').strip()
n = input('修改后的内容:').strip()
a(add,o,n)


2、编写tail工具
import time
def tail(add):
with open(r'%s'%add,mode='rb') as f:
f.seek(0,2)
while True:
line = f.readline()
if len(line) == 0:
time.sleep(0.2)
else:
print(line.decode('utf-8'),end='')

add = input('目标文件:').strip()
tail(add)


3、编写登录功能

def d():
while True:
u = input('请输入账号:').strip()
p = input('请输入密码:').strip()
with open('8_1',mode='rt',encoding='utf-8') as f:
for line in f:
user , pwd = line.strip().split(':')
if u == user and p == pwd:
print('登录成功')
return 1
else:
print('登录失败')

d()



4、编写注册功能
def z():
while True:
nu = input('请输入账号:').strip()
with open('8_1',mode='rt',encoding='utf-8') as f1:
ouser = []
for line in f1:
ou , op = line.strip().split(':')
ouser.append(ou)
if nu in ouser:
print('用户名已存在')
else:
break
while True:
np1 = input('请输入密码:').strip()
np2 = input('请确认密码:').strip()
if np1 == np2:
with open('8_1',mode='at',encoding='utf-8') as f2:
f2.write('%s:%s\n'%(nu,np1))
f2.flush()
return 1
else:
print('密码不相同')

z()



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

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

#登录
def d(tu):
while tu == 0:
u = input('请输入账号:').strip()
p = input('请输入密码:').strip()
with open('db',mode='rt',encoding='utf-8') as f:
for line in f:
user , pwd , money = line.strip().split(':')
if u == user and p == pwd:
print('登录成功')
return u
else:
print('登录失败')

#充值
def c(tu):
c = input('充多少').strip()
import os
with open('db', mode='rt', encoding='utf-8') as read_f,\
open('.db',mode='wt',encoding='utf-8') as write_f:
for line in read_f:
user, pwd, money = line.strip().split(':')
if tu == user:
write_f.write('%s:%s:%s\n'%(user,pwd,int(c)+int(money)))
else:
write_f.write('%s:%s:%s\n' % (user, pwd, money))
os.remove('db')
os.rename('.db','db')
print('充值成功')
return



#转账
def z(tu):
while True:
zu = input('请转入账号:').strip()
with open('db',mode='rt',encoding='utf-8') as f1:
ouser = []
for line in f1:
user, pwd, money = line.strip().split(':')
ouser.append(user)
if zu not in ouser:
print('用户名不存在')
else:
z = input('转多少').strip()
import os
with open('db', mode='rt', encoding='utf-8') as read_f,\
open('.db',mode='wt',encoding='utf-8') as write_f:
for line in read_f:
user, pwd, money = line.strip().split(':')
if tu == user:
write_f.write('%s:%s:%s\n'%(user,pwd,int(money) - int(z)))
elif zu == user:
write_f.write('%s:%s:%s\n' % (user, pwd, int(money) + int(z)))
else:
write_f.write('%s:%s:%s\n' % (user, pwd, money))
os.remove('db')
os.rename('.db','db')
print('转账成功')
return



#提现
def t(tu):
c = input('取多少').strip()
import os
with open('db', mode='rt', encoding='utf-8') as read_f,\
open('.db',mode='wt',encoding='utf-8') as write_f:
for line in read_f:
user, pwd, money = line.strip().split(':')
if tu == user:
write_f.write('%s:%s:%s\n'%(user,pwd,int(money) - int(c)))
else:
write_f.write('%s:%s:%s\n' % (user, pwd, money))
os.remove('db')
os.rename('.db','db')
print('取款成功')
return


#查余
def cy(tu):
with open('db',mode='rt',encoding='utf-8') as f1:
for line in f1:
user, pwd, money = line.strip().split(':')
if user == tu:
print('%s剩余%s'%(tu,money))
input()
return



# 本体

while True:
msg = """
0 退出
1 登录
"""
print(msg)
tu = 0
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue

if cmd == '0':
break
elif cmd == '1':
# 登录
if tu == 0:
# d(tu)
tu = d(tu)

while True:
msg = """
0 退出
1 充值
2 转账
3 提现
4 查询余额
"""
print(msg)
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue

if cmd == '0':
break
elif cmd == '1':#充值
c(tu)

elif cmd == '2':#转账
z(tu)

elif cmd == '3': # 提现
t(tu)

elif cmd == '4': # 查余
cy(tu)

else:
print('输入的命令不存在')
else:
print('输入的命令不存在')
posted @ 2020-06-15 20:24  最冷不过冬夜  阅读(138)  评论(0编辑  收藏  举报