作业 —— day13

一:必做题

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

def update(address,old_content,new_content):
    with open(address,'r+',encoding='utf-8') as f:
        t = f.read().replace(old_content,new_content)
        f.seek(0,0)
        f.write(t)
addr = input('请输入文件地址:')
old = input('请输入原内容:')
new = input('请输入替换内容:')

update(addr,old,new)

2.编写tail工具

import time     # 导入时间模块

def input_info():
    with open('access.log', mode='at', encoding='utf-8') as f1: # at为追加写模式
        inp_info = input('请输入要存入的信息:\n')
        f1.write('{}\n'.format(inp_info))
        
def watch():
    with open('access.log', mode='rb') as f:
        f.seek(0,2) # 把指针移动到结尾
        while True:
            line=f.readline()
            input_info()
            if len(line) == 0:
                time.sleep(1)
            else:
                print(line.decode('utf-8'),end='')

watch()

3.编写登录功能

def login(username,password):
    with open(r'userinfo.txt','r',encoding='utf-8') as f:
        for line in f:
            usr,pwd = line.strip().split(':')
            if username == usr and password == pwd:
                print('登录成功!')
                break
            else:
                print('用户名或密码错误!')

usr = input('请输入帐号:').strip()
pwd = input('请输入密码:').strip()
login(usr,pwd)

4.编写注册功能

def register(username,password):
    with open(r'userinfo.txt','a+t',encoding='utf-8') as f:
        for line in f:
            usr,pwd = line.strip().split(':')
            if username == usr:
                print('该帐号已存在!')
                break
            else:
                f.write('{}:{}\n'.format(username, password))
                print('登录成功!')

usr = input('请输入帐号:').strip()
pwd = input('请输入密码:').strip()
register(usr,pwd)

二:选做题

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

三:选做题中的选做题:登录功能

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

posted @ 2020-03-17 21:29  轻描丨淡写  阅读(191)  评论(0编辑  收藏  举报