3/25 本周选做题

def auth(path):
    from functools import wraps
    global money

    def outter1(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            import time
            time1 = time.strftime('%Y-%m-%d %X')
            func()
            print(func.__name__, type(func.__name__))
            with open(r'{}'.format(path), 'a', encoding='utf-8') as f:
                if func.__name__ == 'money_in':
                    f.write('{} {} 充值 {}
'.format(time1, user_in, money))
                if func.__name__ == 'money_out':
                    f.write('{} {} 消费 {} 元
'.format(time1, user_in, money))

        return wrapper

    return outter1


def log_in():
    import os, time
    global login
    global user_in
    while True:
        user = input('请输入账号:').strip()
        if os.path.exists('{}'.format(user)):
            with open('{}'.format(user), 'r', encoding='utf-8') as f:
                l = f.read()
            if float(l) > time.time():
                print('该用户被锁定了')
                continue
            else:
                os.remove('{}'.format(user))
        with open('db.txt', 'r', encoding='utf-8') as f5:
            for line in f5:
                user1, pwd1, money1 = line.strip().split(':')
                if user == user1:
                    count = 0
                    while count < 3:
                        pwd = input('请输入密码:').strip()
                        if pwd == pwd1:
                            login = True
                            user_in = user
                            print('登陆成功')
                            return 1
                        else:
                            count += 1
                            print('登录失败')
                    with open('{}'.format(user), 'w', encoding='utf-8') as f:
                        f.write(str(time.time() + 300))
                    print('该用户已经被锁定了,傻叉')
                    login = False
                    return login
            else:
                print('没有这个用户哦')


def outter(func):
    from functools import wraps
    @wraps(func)
    def wrapper(*args, **kwargs):
        while True:
            if login:
                func()
                break
            else:
                print('请登录后操作')
                log_in()

    return wrapper


def register():
    tag = True
    while tag:
        tag1 = True
        user = input('请输入账号:').strip()
        if user:
            with open('db.txt', 'r', encoding='utf-8') as f:
                for i in f:
                    i, *_ = i.strip().split(':')
                    if i == user:
                        tag1 = False
                        print('该用户已存在...')
                        break
            if tag1:
                pwd = input('请输入密码:').strip()
                pwd1 = input('请再次输入确认密码:').strip()
                if pwd:
                    if pwd == pwd1:
                        while tag:
                            money = input('请输入你要存的金额:').strip()
                            if money.isdigit():
                                with open('db.txt', 'a', encoding='utf-8') as f5:
                                    f5.write('{}:{}:{}
'.format(user, pwd, money))
                                print('注册成功')
                                tag = False
                            else:
                                print('金额输入有误,请输入整数')
                    else:
                        print('密码不一致')
                else:
                    print('密码不能为空哦')
        else:
            print('账号不能为空哦')


@auth('daliy.txt')
@outter
def money_in():
    while True:
        money = input('请输入充值金额:').strip()
        if money.isdigit():
            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, pwd1, money1 = line.strip().split(':')
                    if user_in == user1:
                        money1 = int(money1)
                        money1 += int(money)
                    f1.write('{}:{}:{}
'.format(user1, pwd1, money1))
                print('充值成功,充值金额为:{}'.format(money))

        else:
            print('请输入整数数字')
            continue
        os.remove('db.txt')
        os.rename('.db.txt.swap', 'db.txt')
        return 1


@auth('daliy.txt')
@outter
def money_out():
    global money
    while True:
        if str(money).isdigit():
            with open('db.txt', 'r', encoding='utf-8') as f, 
                    open('.db.txt.swap', 'w', encoding='utf-8') as f2:
                for i in f:
                    user1, pwd1, money1 = i.strip().split(':')
                    if user1 == user_in:
                        money1 = int(money1)
                        money = int(money)
                        if money < money1:
                            money1 -= money
                            print('购买成功,金额为:{}'.format(money))
                        else:
                            print('没那么多钱')
                    f2.write('{}:{}:{}
'.format(user1, pwd1, money1))
            os.remove('db.txt')
            os.rename('.db.txt.swap', 'db.txt')
            return 1
        else:
            print('请输入整数数字,傻叉')


@outter
def readbook1():
    global money
    while True:
        print('''--------------------------------
    0 玄幻武侠
    1 都市爱情
    2 高效养猪36技
--------------------------------    ''')
        with open('story_class.txt', 'r', encoding='utf-8') as f:
            d = eval(f.read())
        choice1 = input('请输入命令编号:').strip()
        if choice1.isdigit():
            if choice1 in d:
                while True:
                    for key1, value1 in d[choice1].items():
                        print('{} {} 价格:{}'.format(key1, value1[0], value1[1]))
                    choice2 = input('请输入命令编号:').strip()
                    if choice2.isdigit():
                        if choice2 in d[choice1]:
                            print('--------------------------------')
                            choice3 = input('是否付费:').strip()
                            print('--------------------------------')
                            if choice3.upper() == 'Y':
                                money = d[choice1][choice2][1]
                                money_out()
                            break
                        print('no book')
                    else:
                        print('请输入整数数字')
            else:
                print('no book')
        else:
            print('请输入整数数字')


if __name__ == '__main__':
    import os, time

    money = 0
    use_in = None
    login = False
    dic = {'0': register, '1': money_in, '2': readbook1}
    while True:
        print('''--------------------------------
        0 账号注册
        1 充值功能
        2 阅读小说
--------------------------------        ''')
        choice = input('请输入命令编号:').strip()
        if choice.isdigit():
            if choice in ['1', '2', '3']:
                dic[choice]()
            else:
                print('没有这个编号,傻叉')
        else:
            print('请输入整数数字')
原文地址:https://www.cnblogs.com/pythonwl/p/12569293.html