作业 3/17

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

def replace_file(source, l_source, l_aim):
    import os
    with open(r'{}'.format(source), 'r', encoding='utf-8') as f, 
            open('.{}.swap'.format(source), 'w', encoding='utf-8') as f1:
        for line in f:
            print(line)
            line1 = line.replace(l_source, l_aim)
            f1.write(line1)
    os.remove('{}'.format(source))
    os.rename('.{}.swap'.format(source), '{}'.format(source))
replace_file('users.txt','1','#')
2、编写tail工具
def tail(path1):
    import time
    with open('{}'.format(path1),'rb') as f:
        f.seek(0,2)
        while True:
            l = f.readline()
            if l:
                print(l)
            else:
                time.sleep(0.3)
tail('users.txt')

3、编写登录功能

def login():
    user = input('请输入账号:').strip()
    pwd = input('请输入密码:').strip()
    with open('users.txt','r',encoding='utf-8') as f:
        for line in f:
            user1, pwd1 = line.strip().split(':')
            if user == user1 and pwd == pwd1:
                print('登陆成功')
                break
        else:
            print('登陆失败')
login()

4、编写注册功能

def register():
    user = input('请输入账号:').strip()
    if user:
        pwd = input('请输入密码:').strip()
        pwd1 = input('请再次确认输入密码:').strip()
        if pwd == pwd1:
            with open('users.txt','a',encoding='utf-8') as f:
                f.write('{}:{}
'.format(user,pwd))
            print('注册成功')
        else:
            print('密码不一致')
    else:
        print('用户名不能为空')
register()

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

import os


def top_up(user_login):
    money_in = 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:
            user, money = line.strip().split(':')
            if user == user_login:
                money = int(money)
                money += int(money_in)
                print('充值成功:{}'.format(money_in))
            line1 = '{}:{}
'.format(user, money)
            f1.write(line1)
    os.remove('db.txt')
    os.rename('.db.txt.swap', 'db.txt')


def transfer_money(user_source, user_aim):
    with open('db.txt', 'r', encoding='utf-8') as f, 
            open('.db.txt.swap', 'w', encoding='utf-8') as f1:
        for ll in f:
            user_l, money1000 = ll.strip().split(':')
            if user_aim == user_l:
                money = input('转账金额:')
                f.seek(0)
                l = f.readlines()
                f.seek(0)
                for line in f:
                    user, money1 = line.strip().split(':')
                    money1 = int(money1)
                    money = int(money)
                    if user == user_source:
                        if money > money1:
                            print('没那么多钱')
                        else:
                            money1 -= int(money)
                    if user == user_aim:
                        for i in l:
                            user5, money5 = i.strip().split(':')
                            if user5 == user_source:
                                if int(money5) > money:
                                    money1 = int(money1)
                                    money1 += int(money)
                    f1.write('{}:{}
'.format(user, money1))
                break
        else:
            print('没有这个用户哦')
            return
    os.remove('db.txt')
    os.rename('.db.txt.swap', 'db.txt')


def money_out(user):
    money = input('提现金额:').strip()
    with open('db.txt','r',encoding='utf-8') as f,
            open('.db.txt.swap','w',encoding='utf-8') as f1:
        for line in f:
            user1, money1 = line.strip().split(':')
            if user == user1:
                money1 = int(money1)
                if int(money) > money1:
                    print('没那么多钱')
                else:
                    money1 -= int(money)
                    print('提现成功:{}'.format(money))
            line1 = '{}:{}
'.format(user1,money1)
            f1.write(line1)
    os.remove('db.txt')
    os.rename('.db.txt.swap','db.txt')


def look_at_it(user):
    with open('db.txt','r',encoding='utf-8') as f:
        for line in f:
            user1, money = line.strip().split(':')
            if user == user1:
                print('余额:{}'.format(money))
# 选做题中的选做题:登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作
def log_in():
    user = input('请输入账号:').strip()
    with open('users.txt','r',encoding='utf-8') as f:
        for line in f:
            user1, pwd1 = line.strip().split(':')
            if user == user1:
                pwd = input('请输入密码:').strip()
                if pwd == pwd1:
                    print('登陆成功')
                    return True,user
                else:
                    print('登陆失败')
                    break
        else:
            print('没有这个用户哦')
    return 0,0


if __name__ == '__main__':
    tag = [False,]
    while True:
        tag = log_in()
        user = tag[1]
        if tag[0]:
            break
    while tag[0]:
        l = ['充值','转账','提现','查询余额','退出']
        print('''===========================
        0 {}
        1 {}
        2 {}
        3 {}
        4 {}
==========================='''.format(l[0],l[1],l[2],l[3],l[4]))
        pwd = input('请输入命令:').strip()
        if pwd.isdigit():
            if pwd == '0':
                top_up(user)
            if pwd == '1':
                user_aim = input('你想转给谁:').strip()
                transfer_money(user,user_aim)
            if pwd == '2':
                money_out(user)
            if pwd == '3':
                look_at_it(user)
            if pwd == '4':
                break
原文地址:https://www.cnblogs.com/pythonwl/p/12513129.html