zoukankan      html  css  js  c++  java
  • python第十三天作业

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

    def file_fuc(x, y, z):
        import os
        with open(r'{}'.format(x), 'rb') as rf, 
                open(r'{}.swap'.format(x), 'wb') as wf:
            while True:
                res = rf.readline().decode('utf-8')
                if y in res:
                    data = res.replace('{}'.format(y), '{}'.format(z))
                    wf.write(data.encode('utf-8'))
                else:
                    wf.write('{}'.format(res).encode('utf-8'))
                if len(res) == 0:
                    break
        os.remove(r'{}'.format(x))
        os.rename(r'{}.swap'.format(x), r'{}'.format(x))
        return '修改成功'
    
    path_file = input('修改的文件路径:>>').strip()
    file1_info = input('要修改的内容:>>').strip()
    file2_info = input('修改后的内容:>>').strip()
    if path_file and file1_info and file2_info:
        msg = file_fuc(path_file,file1_info,file2_info)
        print(msg)
    else:
        print('输入格式失败')
    

    2、编写tail工具

    def tail(x):
        import time
        import os
        if not os.path.exists('{}'.format(x)):
            with open('{}'.format(x), 'wb') as f:
                f.write('创建该日志文件
    '.encode('utf-8'))
        with open(r'{}'.format(x), 'rb') as f1:
            f1.seek(0, 2)
            while True:
                a = f1.readline().decode('utf-8')
                if len(a) == 0:
                    time.sleep(1)  # 因为自动写入速度太快了,所以给他控制个时间
                    test(x)
                else:
                    print(a)
    
    
    def test(x):
        with open(r'{}'.format(x), 'ab') as wf:
            wf.write('2020114455001 egon转账200w
    '.encode('utf-8'))
    
    
    msg = input('请输入需要观察的日志目录:>>')
    tail(msg)
    

    3、编写登录功能

    def login(user,pwd):
        with open(r'db.txt', 'rt', encoding='utf-8') as f:
            for line in f:
                x,y = line.strip().split(':')
                if user == x and pwd == y:
                    print('登录成功')
                else:
                    print('失败')
    
    
    username = input('账号:>>').strip()
    password = input('密码:>>').strip()
    login(username,password)
    

    4、编写注册功能

    def register(x, y):
        with open(r'db.txt', 'at', encoding='utf-8') as f:
            for line in f:
                user, pwd = line.strip().split(':')
                if x == user:
                    print("用户名已存在")
                    break
            else:
                f.write('{}:{}
    '.format(x, y))
    
    username = input("用户名:>>").strip()
    password = input("密码:>>").strip()
    register(username, password)
    

    选做题

    编写ATM程序实现下述功能,数据来源于文件db.txt

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

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

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

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

    def login(user, pwd):
        with open(r'db.txt', 'rt', encoding='utf-8') as f:
            for line in f:
                x, y, z = line.strip().split(':')
                if user == x and pwd == y:
                    print('登录成功')
                    return 1
            else:
                print('失败')
            return 0
    
    
    def recharge_fuc(name, money):  # 充值功能
        with open(r'db.txt', 'rb') as rf, 
                open(r'db.txt.swap', 'wb') as wf:
            while True:
                user_msg = rf.readline().decode('utf-8')
                *_, z = user_msg.strip().split(':')
                if name in user_msg:
                    money = int(z) + int(money)
                    data = user_msg.replace('{}'.format(z), '{}'.format(money))
                    wf.write(data.encode('utf-8'))
                else:
                    wf.write('{}'.format(user_msg).encode('utf-8'))
                if len(user_msg) == 0:
                    break
        move_fuc()
        return '充值成功'
    
    
    def take_fuc(username1, username2, money):  # 转账功能
        with open(r'db.txt', 'rb') as rf, 
                open(r'db.txt.swap', 'wb') as wf:
            while True:
                usert1_msg = rf.readline().decode('utf-8')
                *_, z = usert1_msg.strip().split(':')
                if username1 in usert1_msg:
                    if int(z) > int(money):
                        money1 = int(z) - int(money)
                        data = usert1_msg.replace('{}'.format(z), '{}'.format(money1))
                        wf.write(data.encode('utf-8'))
                    else:
                        return '转账现金已经超过余额'
                else:
                    wf.write('{}'.format(usert1_msg).encode('utf-8'))
                if len(usert1_msg) == 0:
                    rf.seek(0, 0)
                    break
        move_fuc()
        with open(r'db.txt', 'rb') as rf, 
                open(r'db.txt.swap', 'wb') as wf:
            while True:
                usert2_msg = rf.readline().decode('utf-8')
                *_, z = usert2_msg.strip().split(':')
                if username2 in usert2_msg:
                    money2 = int(z) + int(money)
                    data = usert2_msg.replace('{}'.format(z), '{}'.format(money2))
                    wf.write(data.encode('utf-8'))
                else:
                    wf.write('{}'.format(usert2_msg).encode('utf-8'))
                if len(usert2_msg) == 0:
                    break
        move_fuc()
        return '转账成功'
    
    
    def cash_fuc(name,money):  # 提现功能
        with open(r'db.txt', 'rb') as f, 
                open(r'db.txt.swap', 'wb') as wf:
            while True:
                cash_msg = f.readline().decode('utf-8')
                if name in cash_msg:
                    *_ , z = cash_msg.strip().split(':')
                    if int(z) > int(money):
                        money1 = int(z) - int(money)
                        data = cash_msg.replace('{}'.format(z), '{}'.format(money1))
                        wf.write(data.encode('utf-8'))
                    else:
                        return '提现失败'
                else:
                    wf.write('{}'.format(cash_msg).encode('utf-8'))
                if len(cash_msg) == 0:
                    break
        move_fuc()
        return '提现成功'
    
    
    def query_fuc(name):  # 查询余额功能
        with open(r'db.txt','rb') as f:
            while True:
                query_msg = f.readline().decode('utf-8')
                if name in query_msg:
                    *_ , z = query_msg.strip().split(':')
                    return z
    
    
    def move_fuc():
        import os
        os.remove(r'{}'.format('db.txt'))
        os.rename(r'{}.swap'.format('db.txt'), r'{}'.format('db.txt'))
    
    
    msg_info = {
        '1': '充值功能',
        '2': '转账功能',
        '3': '提现功能',
        '4': '查询余额功能',
    }
    username = input('请输入您的账号:>>').strip()
    password = input('请输入您的密码:>>').strip()
    tag = login(username, password)
    while tag:
        key_msg = '''
            1: 充值功能
            2: 转账功能
            3: 提现功能
            4: 查询余额功能
        '''
        print(key_msg)
        cmd = input('请输入命令编号>>: ').strip()
        if not cmd.isdigit():
            print('必须输入命令编号的数字,傻叉')
            continue
        if cmd not in msg_info:
            print('输入的命令不存在')
            continue
        if msg_info[cmd] == '充值功能':
            money_inp_re = input('请输入您的充值金额:>>').strip()
            res_1 = recharge_fuc(username,money_inp_re)
            print(res_1.center(10,'='))
        elif msg_info[cmd] == '转账功能':
            username_take = input('请输入您的转账账号:>>').strip()
            money_inp_tk = input('请输入您的充值金额:>>').strip()
            res_2 = take_fuc(username,username_take,money_inp_tk)
            print(res_2.center(10,'='))
        elif msg_info[cmd] == '提现功能':
            money_inp_ca = input('请输入您的提现金额:>>').strip()
            res_3 = cash_fuc(username,money_inp_ca)
            print(res_3.center(10,'='))
        elif msg_info[cmd] == '查询余额功能':
            print(query_fuc(username).center(10,'='))
    
  • 相关阅读:
    CodeForces 347B Fixed Points (水题)
    CodeForces 347A Difference Row (水题)
    CodeForces 346A Alice and Bob (数学最大公约数)
    CodeForces 474C Captain Marmot (数学,旋转,暴力)
    CodeForces 474B Worms (水题,二分)
    CodeForces 474A Keyboard (水题)
    压力测试学习(一)
    算法学习(一)五个常用算法概念了解
    C#语言规范
    异常System.Threading.Thread.AbortInternal
  • 原文地址:https://www.cnblogs.com/Lance-WJ/p/12512302.html
Copyright © 2011-2022 走看看