• day13 作业


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

    import os
    def file_update(file_path,old_msg,new_msg):
        with open("b.txt","r",encoding="utf-8") as f 
                ,open(f"{file_path}\b.txt","w",encoding="utf-8") as f1:
            for line in f:
                f1.write(line.replace(f"{old_msg}",f"{new_msg}"))
        os.remove("b.txt")
    file_update("locked","a","b")
    

    2、编写tail工具

    import time
    def tail():
        with open("log.txt","rb") as f:
            f.seek(0,2)
            while True:
                new_msg = f.readline()
                if len(new_msg)==0:
                    time.sleep(1)
                else:
                    print(new_msg.decode("utf-8"))
    tail()
    #另一个文件
    def write_log():
        with open("log.txt","a",encoding="utf-8") as f:
            msg = input("请输入日志内容")
            f.write(f"
    {msg}")
    write_log()
    

    3、编写登录功能

    def login():
        user_inp = input("your name >")
        pwd_inp = input("your msg >")
        with open("a.txt","r",encoding="utf-8") as f:
            for line in f :
                username,password = line.strip().split(":")
                if username==user_inp and pwd_inp == password:
                    print("登录成功")
                    break
            else:
                print("输入错误")
    login()
    

    4、编写注册功能

    def register():
        username = input("请输入注册的用户名:")
        password = input("请输入注册的密码:")
        with open("a.txt","a",encoding="utf-8") as f :
            f.write(f"
    {username}:{password}")
    register()
    

    选做题:编写ATM程序实现以下功能,数据源于文件db.txt

    1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改

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

    3、提现功能:用户输入提现金额,db.txt中该账号钱数减少

    4、查询余额功能:输入账号查询余额

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

    import os
    def recharge():
        ''' 充值功能'''
        name_inp = input("请输入要充值的账号>")
        money_inp = input("请输入要充值的金钱>")
        with open("db.txt","r",encoding="utf-8") as f ,
            open(".db.txt.swap","w",encoding="utf-8") as f1:
                for line in f :
                    if name_inp in line :
                        name,money = line.strip().split(":")
                        new_money = int(money_inp)+int(money)
                        f1.write(line.replace(money,str(new_money)))
                    else:
                        f1.write(line)
        os.remove("db.txt")
        os.rename(".db.txt.swap","db.txt")
    
    def transfer():
        '''转账功能'''
        transfer_name_inp = input("请输入转账人姓名:")
        collector_name_inp = input("请输入收账人姓名:")
        money_inp = input("请输入转账金额")
        with open("db.txt","r",encoding="utf-8") as f ,
            open(".db.txt.swap","w",encoding="utf-8") as f1:
            for line in f :
                if transfer_name_inp in line :
                    transfer_name,transfer_money=line.strip().split(":")
                    new_money = int(transfer_money)-int(money_inp)
                    f1.write(line.replace(transfer_money,str(new_money)))
                elif collector_name_inp in line:
                    collector_name,collector_money=line.strip().split(":")
                    new_money = int(collector_money)+int(money_inp)
                    f1.write(line.replace(collector_money,str(new_money)))
                else:
                    f1.write(line)
        os.remove("db.txt")
        os.rename(".db.txt.swap","db.txt")
    def cash_withdrawal():
        '''提现功能'''
        name_inp = input("请输入提现账号的姓名:")
        money_inp = input("请输入提现的金额:")
        with open("db.txt","r",encoding="utf-8") as f ,
            open(".db.txt.swap","w",encoding="utf-8") as f1:
            for line in f:
                if name_inp in line:
                    name,money = line.strip().split(":")
                    new_money = int(money)-int(money_inp)
                    f1.write(line.replace(money,str(new_money)))
                else:
                    f1.write(line)
        os.remove("db.txt")
        os.rename(".db.txt.swap","db.txt")
    
    def login():
        '''登录功能'''
        user_inp = input("your name >")
        pwd_inp = input("your msg >")
        with open("a.txt","r",encoding="utf-8") as f:
            for line in f :
                username,password = line.strip().split(":")
                if username==user_inp and pwd_inp == password:
                    print("登录成功")
                    return 1
            else:
                print("输入错误")
    
    def query():
        '''查询功能'''
        name_inp=input("请输入你要查询的用户名:")
        with open("db.txt","r",encoding="utf-8")as f :
            for line in f :
                name,money = line.strip().split(":")
                if name_inp in line:
                    print(money)
                    break
    
    #主程序
    list1=["1","2","3","4"]
    while True:
        res = login()
        if res:
            while True:
                print('''
                       欢迎来到只能充值转账提现的劣质ATM系统
                                   1.充值
                                   2.转账
                                   3.提现
                                   4.查询
                       ''')
                cmd = input("请输入指令>")
                if cmd ==list1[0]:
                    recharge()
                elif cmd == list1[1]:
                    transfer()
                elif cmd == list1[2]:
                    cash_withdrawal()
                elif cmd == list1[3]:
                    query()
                else:
                    print("非法输入")
    
    
  • 相关阅读:
    个人作业——软件评测
    软件工程实践2019第五次作业
    18年今日头条笔试第一题题解:球迷(fans)
    游戏2.1版本
    游戏2.0版本 代码
    游戏2.0版本
    改进版游戏代码
    改进版游戏
    2017.1.13之审判日
    找朋友 的内存超限代码
  • 原文地址:https://www.cnblogs.com/hz2lxt/p/12511480.html
Copyright © 2020-2023  润新知