zoukankan      html  css  js  c++  java
  • 作业18

    作业18

    # ====================本周选做作业如下====================
    # 编写小说阅读程序实现下属功能
    # # 一:程序运行开始时显示
    #     0 账号注册
    #     1 充值功能
    #     2 阅读小说
    
    # # 二: 针对文件db.txt,内容格式为:"用户名:密码:金额",完成下述功能
    # 2.1、账号注册
    # 2.2、充值功能
    # # 三:文件story_class.txt存放类别与小说文件路径,如下,读出来后可用eval反解出字典
    # {"0":{"0":["倚天屠狗记.txt",3],"1":["沙雕英雄转.txt",10]},"1":{"0":["令人羞耻的爱.txt",6],"1":["二狗的妻子与大草原的故事.txt",5]},}
    # 3.1、用户登录成功后显示如下内容,根据用户选择,显示对应品类的小说编号、小说名字、以及小说的价格
    # """
    # 0 玄幻武侠
    # 1 都市爱情
    # 2 高效养猪36技
    # """
    #
    # 3.2、用户输入具体的小说编号,提示是否付费,用户输入y确定后,扣费并显示小说内容,如果余额不足则提示余额不足
    
    # 四:为功能2.2、3.1、3.2编写认证功能装饰器,要求必须登录后才能执行操作
    
    # 五:为功能2.2、3.2编写记录日志的装饰器,日志格式为:"时间 用户名 操作(充值or消费) 金额"
    
    # 附加:
    # 可以拓展作者模块,作者可以上传自己的作品
    
    #     0 账号注册
    #     1 充值功能
    #     2 阅读小说
    
    
    
    
    
    
    
    
    
    
    from functools import wraps
    # ----------------------日志装饰器-------------------------------------
    import time
    def log(func):
        @wraps(func)
        def wrapper(*args,**kwargs):
            res = func(*args,**kwargs)
            if res:
                if func.__name__ == "invest":
                    operation = "充值"
                elif func.__name__ == "book_charge":
                    operation = "消费"
                with open("user_log.txt","a",encoding="utf-8") as f:
                    f.write("{} {} {} {}
    ".format(time.strftime('%Y-%m-%d %H:%M:%S'),online_user,operation,res))
            return res
        return wrapper
    
    
    # -------------------------认证装饰器------------------------
    import time
    online_user = None
    online_time = 0
    def identify(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            global online_user, online_time
            now_time = time.time()
            if not (online_user and now_time - online_time <= 180):
                inp_user = input("请先登录,输入用户名:")
                inp_password = input("请输入密码:")
                with open("db.txt", "r", encoding="utf-8") as f:
                    for line in f:
                        user, password,_ = line.strip().split(":")
                        if user == inp_user and password == inp_password:
                            print("登录成功")
                            online_user = inp_user
                            online_time = time.time()
    
                            break
                        elif user == inp_user and password != inp_password:
                            print("密码错误")
                            return
                    else:
                        print("用户名不存在")
                        return
            res = func(*args, **kwargs)
            return res
    
        return wrapper
    
    
    
    
    
    
    
    # ------------------------注册功能-------------------------------
    def logon():
        inp_user = input("请输入要注册的用户名:")
        with open("db.txt", "r", encoding="utf-8") as f:
            for line in f:
                user, _, _ = line.strip().split(":")
                if user == inp_user:
                    print("用户名已存在,不能注册")
                    break
            else:
                inp_password = input("请输入密码:")
                with open("db.txt", "a", encoding="utf-8") as f1:
                    f1.write("{}:{}:0
    ".format(inp_user, inp_password))
                print("注册成功")
    
    
    # logon()
    
    
    
    # -----------------------充值功能------------------------------
    import os
    
    @log
    @identify
    def invest():
        inp_money = input("请输入要充值多少:")
        if inp_money.isdigit():
            inp_money = int(inp_money)
            tag = 0
            with open("db.txt", "r", encoding="utf-8") as f1, 
                    open("db.txt.swap", "w", encoding="utf-8") as f2:
                for line in f1:
                    user, password, money = line.strip().split(":")
                    if user == online_user:
                        money = str(float(money) + inp_money)
                        f2.write("{}:{}:{}
    ".format(user, password, money))
                        tag = 1
                    else:
                        f2.write(line)
                else:
                    if tag == 0:
                        print("充值失败,没有此用户")
            os.remove("db.txt")
            os.rename("db.txt.swap", "db.txt")
            if tag == 0:
                return
            elif tag == 1:
                return inp_money
        else:
            print("充值数量必须为数字")
            return
    
    
    # invest()
    
    
    # --------------扣费功能---------------------------------------------
    
    dic = {"0": {"0": ["倚天屠狗记.txt", 3], "1": ["沙雕英雄转.txt", 10]},
           "1": {"0": ["令人羞耻的爱.txt", 6], "1": ["二狗的妻子与大草原的故事.txt", 5]}, }
    online_user = "wu"
    import os
    
    @log
    def book_charge(type_choose):
        book_choose = input("请输入查看的小说:")
        price = dic[type_choose][book_choose][1]
        book = dic[type_choose][book_choose][0]
        print("您选择的小说价格为{}".format(price))
        result = input("是否付费阅读:(输入y确定,n取消)").strip()
        if result.upper() == "Y":
            tag = 0
            with open("db.txt", "r", encoding="utf-8") as f1, 
                    open("db.txt.swap", "w", encoding="utf-8") as f2:
                for line in f1:
                    user, password, balance = line.strip().split(":")
                    if user == online_user:
                        new_balance = float(balance) - float(price)
                        if new_balance >= 0:
                            balance = str(new_balance)
                            f2.write("{}:{}:{}
    ".format(user, password, balance))
                            print("扣费成功,请阅读")
                            tag = 1
                            with open(book,"r",encoding="utf-8") as b:
                                for line in b:
                                    print(line.strip())
                        else:
                            print("余额不足,无法付费")
                            f2.write(line)
                    else:
                        f2.write(line)
            os.remove("db.txt")
            os.rename("db.txt.swap", "db.txt")
            if tag == 0:
                return
            elif tag == 1:
                return price
        elif result.upper() == "N":
            print("返回")
            return
    
    
    # --------------选择小说功能------------------------------
    @identify
    def choose():
        while 1:
            print("0 玄幻武侠
    1 都市爱情
    2 高效养猪36技")
            type_choose = input("请输入查看的小说类型:")
            if not type_choose.isdigit():
                print("只能输入数字")
                continue
            elif type_choose in dic:
                for num, book_info in dic[type_choose].items():
                    print("序号:{} 书名:{} ".format(num, book_info[0]).ljust(35, "-"), "价格:{}".format(book_info[1]))
                return book_charge(type_choose)
    
    # choose()
    
    
    # ---------------------页面显示-------------------------------
    
    
    def display():
        for k,v in func_dic.items():
            print(k,v[0])
        cmd = input("请选择功能:").strip()
        if cmd.isdigit():
            if cmd in func_dic:
                func_dic[cmd][1]()
            else:
                print("功能不存在")
        else:
            print("只能输入数字")
    # display()
    func_dic = {
        "0": ["退出", exit],
        "1": ["注册", logon],
        "2": ["充值", invest],
        "3": ["阅读小说",choose],
    }
    
    
    # ------------------------主程序-----------------------
    def main():
        while 1:
            display()
    
    main()
    
  • 相关阅读:
    MySQL语法
    Linux常用命令大全
    触发器使用UTL_SMTP包发送邮件
    MySQL——触发器的创建和使用总结
    MySQL数据库备份
    Nginx配置文件(nginx.conf)配置详解
    JS弹出框,打开文件,保存文件,另存为。。。。
    java excel两种操作方式
    Zookeeper的优缺点
    activemq linux安装
  • 原文地址:https://www.cnblogs.com/achai222/p/12584392.html
Copyright © 2011-2022 走看看