• 对于之前小程序的优化,解决文件没有需要新建问题


    https://www.cnblogs.com/pythonywy/p/10957368.html之前的函数没有自动新建文件功能必须有有文件才能正常运行

    练手:加了注册时候密码两次必须选题,文件没有回自动创建,

    关于open with中r模式不会建文件,我们只要a+然后再把指针移至开头再read,这样就能读,也不会w模式清空文件

    total_prices = 0
    
    def chiose(action):
        '''0是注册功能,1是会员卡系统,2是购物功能,3是会员查找积分功能,4为会员积分兑换功能'''
        #注册功能
        if action == 0:
            # 注册内容
            def register():
                while True:
                    print('注册账号'.center(50, "-"))
                    name = input('请输入账号').strip()
                    print('注册密码'.center(50, "-"))
                    pwd = input('请输入密码').strip()
                    pwd_next = input('请再一次输入入密码').strip()
                    with open('用户注册信息.txt', 'a+', encoding='utf8') as fw:
                        fw.seek(0)
                        data = fw.read()
                    if name in data :
                        print('已有账号')
                        continue
                    else:
                        if pwd == pwd_next:
                            print('注册成功')
                            with open('用户注册信息.txt', 'a', encoding='utf8') as fw:
                                fw.write(f'{name}:{pwd}|')
                            break
                        else:
                            print('两次输入密码不同')
                print(50*'-')
            return register
    
        # 会员卡功能,100块钱1分
        # 其中加个必须结合shoping进行
        #小于10分是普通会员9折优惠
        #大于等于10分是黄金会员8.5折优惠
        # 大于等于100分是至尊会员打8折
        if action == 1:
            def member():
                choise = input('有会员输入1
    '
                               '没会员输入其他
    '
                               '最终解释权归杨文益所有')
                count = 0
                if choise == '1':
                    while count == 0:
                        info_dict = dict()
                        member_dict = dict()
                        new_member_dict = dict()
                        print('登入账号'.center(50, "-"))
                        name = input('请输入账号(真的想不起来了,输入我忘了密码随意退出)
    ').strip()
                        print('登入密码'.center(50, "-"))
                        pwd = input('请输入密码').strip()
    
                        with open('用户注册信息.txt', 'a+', encoding='utf8') as fr:
                            fr.seek(0)
                            info = fr.read()
                        if info == '':
                            print('没有注册请先注册')
                            return
                        info_list = info.split('|')
                        for info in info_list[:-1]:
                            info = info.split(':')
                            info_name = info[0]
                            info_pwd = info[1]
                            info_dict[info_name] = info_pwd
                        if name == '我忘了':
                            print(50 * '-')
                            count = 1
                        elif name not in info_dict:
                            print('会员账号错误')
                            continue
                        elif pwd != info_dict.get(name):
                            print('密码错误')
                            continue
                        else:
                            print('会员成功')
                            count = 1
                            # 写入会员名字加积分
    
                            with open('会员积分.txt', 'a', encoding='utf8')  as fw,
                                open('会员积分剩余积分.txt', 'a', encoding='utf8')  as fw_1:
                                fw.write(f'{name}:{total_prices / 100}|')
                                fw_1.write(f'{name}:{total_prices / 100}|')
                            # 读写会员总积分
                            with open('会员积分.txt', 'r', encoding='utf8') as fw:
                                member = fw.read()
                            member_list = member.split('|')
                            for member in member_list[:-1]:
                                member = member.split(':')
                                member_name = member[0]
                                member_integral = float(member[1])
                                if member_name not in member_dict:
                                    member_dict[member_name] = member_integral
                                else:
                                    member_dict[member_name] += member_integral
    
                            # 读写会员剩余积分
                            with open('会员积分剩余积分.txt', 'r', encoding='utf8') as fw:
                                new_member = fw.read()
                            new_member_list = new_member.split('|')
                            for new_member in new_member_list[:-1]:
                                new_member = new_member.split(':')
                                new_member_name = new_member[0]
                                new_member_integral = float(new_member[1])
                                if new_member_name not in new_member_dict:
                                    new_member_dict[new_member_name] = new_member_integral
                                else:
                                    new_member_dict[new_member_name] += new_member_integral
    
                            # 打印积分
                            print(50 * '-')
                            print(f'你好{name}')
                            print(f'您当前的积分{member_dict.get(name)}分')
                            integral = member_dict.get(name)
                            if integral < 10:
                                print(50 * '-')
                                print('您是我们超市的普通会员可以享受9折优惠')
                                print(50 * '-')
                                print(f'你本次购买总计:{total_prices}')
                                print(f'会员折扣后为{0.9*total_prices}')
                                print(50 * '-')
                                print(f'请到前台支付{0.9 * total_prices}')
                                print(50 * '-')
                                print('欢迎下次光临'.center(44,"-"))
                            elif integral < 100:
                                print(50 * '-')
                                print('您是我们超市的黄金会员可以享受8.5折优惠')
                                print(50 * '-')
                                print(f'你本次购买总计:{total_prices}')
                                print(f'会员折扣后为{0.85 * total_prices}')
                                print(50 * '-')
                                print(f'请到前台支付{0.85 * total_prices}')
                                print(50 * '-')
                                print('欢迎下次光临'.center(44, "-"))
                            else:
                                print(50 * '-')
                                print('您是我们超市的至尊会员可以享受8折优惠')
                                print(50 * '-')
                                print(f'你本次购买总计:{total_prices}')
                                print(f'会员折扣后为{0.8 * total_prices}')
                                print(50 * '-')
                                print(f'请到前台支付{0.8 * total_prices}')
                                print(50 * '-')
                                print('欢迎下次光临'.center(44, "-"))
                            print(50 * '-')
    
            return member
    
    
        #购物系统
        if action == 2:
            def shopping():
                # 产品成功选择后用judge = 1跳转到继续,结清,清空选择列表
                # 选择产品后继续 jump == '1'跳转,结算用jump == '0' 跳转, 清空继续购买用jump == '2'跳转,清空退出购买用jump == '3'跳转
                # 用 a == '0' 控制继续购买, a =='1'控制退出购买
                commodity_dict = {
                    '0': ['cat', 100],
                    '1': ['dog', 200],
                    '2': ['pig', 300]
                }
                user_dict = dict()
                a = '0'
                print('欢迎来选购'.center(50, "-"))
                while a == '0':
                    chiose = input('输入0商品是cat
    '
                                   '输入1商品是dog
    '
                                   '输入2商品是pig
    '
                                   '请选择你要购买的商品:')
                    print(50 * '-')
                    commodity_info = commodity_dict.get(chiose)
                    num = input('请输入你选择商品的数量')
                    # 判断输入内容
    
                    if not num.isdigit():
                        print('数量输入有误请重新输入,数量只能输入数字')
                        continue
                    if chiose not in commodity_dict:
                        print('无效商品,请在0,1,2中选择输入')
                        continue
    
                    # 整理商品清单
                    else:
                        num_int = int(num)
                        if commodity_info[0] not in user_dict:
                            user_dict[commodity_info[0]] = [num_int, num_int * commodity_info[1]]
                            judge = 1  # 跳转选择界面
                            a = '1'
    
                        else:
                            user_dict[commodity_info[0]][0] += num_int
                            user_dict[commodity_info[0]][1] += num_int * commodity_info[1]
                            judge = 1  # 跳转选择界面
                            a = '1'
    
                    # 打印购买信息
                    global total_prices
                    total_prices = 0
                    print("
    ")
                    print('杨大爷超市欢迎您'.center(42, '-'))
                    print('
    你选择的商品')
                    for name, info_list in user_dict.items():
                        print(f'{name}{info_list[0]}个合计{info_list[1]}元
    ')
                        total_prices += info_list[1]
                    print(50 * '-')
                    print(f'        总计{total_prices}元')
    
                    # 功能选择
                    while judge == 1:
                        print(50 * '-')
                        jump = input('输入0结算
    '
                                     '输入1继续购买
    '
                                     '输入2清空购物车继续购买
    '
                                     '输入3清空购物车退出
    '
                                     '请输入您的选择').strip()
                        if not jump.isdigit():
                            print('请正确输入0,1,2,3中任意数字')
                            continue
                        if jump == '1':
                            judge = 4  # 跳出功能选择
                            a = '0'  # 继续购买
                        elif jump == '2':
                            user_dict = dict()  # 清空购物车
                            judge = 4  # 跳出功能选择
                            a = '0'  # 继续购买
                        elif jump == '3':
                            user_dict = dict()
                            judge = 4  # 跳出功能选择
                            print("-" * 50)
                            print('欢迎下次光临')
                        elif jump == '0':
                            judge = 4  # 跳出功能选择
                            print("-" * 50)
                            print(f'一共{total_prices}元请到前台支付,如果有会员在下面输入会员信息')
                            print("-" * 50)
                            print('欢迎下次光临')
    
            return shopping
    
        #会员积分查询
        if action == 3:
            def menber_integral():
                #生成总积分字典和用户信息字典:
                menber_integral_dict = dict()
    
                with open('用户注册信息.txt', 'a+', encoding='utf8') as fr_1:
                    fr_1.seek(0)
                    data = fr_1.read()
                if data == '':
                    print('用户未注册请输入0去注册')
                    return
                with open('会员积分.txt','a+',encoding='utf8') as fr:
                    fr.seek(0)
                    info_list = fr.read().split('|')[:-1]
                    for info in info_list:
                        info = info.split(':')
                        if info[0] not in menber_integral_dict:
                            menber_integral_dict[info[0]] = float(info[1])
                        else:
                            menber_integral_dict[info[0]] += float(info[1])
                user_menber_integral_dict = dict()
                with open('用户注册信息.txt','a+',encoding='utf8') as fr:
                    fr.seek(0)
                    user_info_list = fr.read().split('|')[:-1]
                    for user_info in user_info_list:
                        user_info = user_info.split(':')
                        user_menber_integral_dict[user_info[0]] = user_info[1]
    
                # 生成剩余积分字典:
    
                new_menber_integral_dict = dict()
                with open('会员积分剩余积分.txt', 'a+', encoding='utf8') as fr:
                    fr.seek(0)
                    new_info_list = fr.read().split('|')[:-1]
                    for new_info in new_info_list:
                        new_info = new_info.split(':')
                        if new_info[0] not in new_menber_integral_dict:
                            new_menber_integral_dict[new_info[0]] = float(new_info[1])
                        else:
                            new_menber_integral_dict[new_info[0]] += float(new_info[1])
                while True :
                    print('-'*50)
                    name = input('请输入你会员卡的名字(退出输入我忘了)').strip()
                    if name == '我忘了':
                        print('-' * 50)
                        print('请选择')
                        break
                    if not name in user_menber_integral_dict:
                        print('-' * 50)
                        print('会员账号输入错误')
                        print('-' * 50)
                    if name in user_menber_integral_dict and menber_integral_dict == dict():
                        print('你的积分为0分')
                        return
                    if name in menber_integral_dict:
                        print('-' * 50)
                        print('会员介绍'.center(46,' '))
                        print('积分小于10分为普通会员可以打9折
    '
                              '积分大于10分小于100分为黄金会员可以打8.5折
    '
                              '积分大于100分为黄金会员可以打8折')
                        print('-' * 50)
                        print(f'会员卡账号:{name}')
                        if menber_integral_dict[name] <10:
                            print('您当前会员等级为普通会员
    '
                                  '您可以享受9折优惠')
                        elif menber_integral_dict[name] <100:
                            print('您当前会员等级为黄金会员
    '
                                  '您可以享受8.5折优惠')
                        else:
                            print('您当前会员等级为至尊会员
    '
                                  '您可以享受8折优惠')
                        print(f'当前总积分:{menber_integral_dict[name]}分')
                        print(f'已使用积分:{-new_menber_integral_dict[name] + menber_integral_dict[name]}分')
                        print(f'剩余积分:{new_menber_integral_dict[name]}分')
                        break
            return menber_integral
    
        #会员积分兑换系统
        if action == 4:
            def gift():
                count = 0
                # 生成剩余积分字典:
    
                with open('用户注册信息.txt', 'a+', encoding='utf8') as fr_1:
                    fr_1.seek(0)
                    data = fr_1.read()
                if data == '':
                    print('用户未注册')
                    return
                new_menber_integral_dict = dict()
                with open('会员积分剩余积分.txt', 'a+', encoding='utf8') as fr:
                    fr.seek(0)
                    new_info_list = fr.read().split('|')[:-1]
                    for new_info in new_info_list:
                        new_info = new_info.split(':')
                        if new_info[0] not in new_menber_integral_dict:
                            new_menber_integral_dict[new_info[0]] = float(new_info[1])
                        else:
                            new_menber_integral_dict[new_info[0]] += float(new_info[1])
                #礼物字典:
                gift_dict = {
                    '0':['钢笔',1.0],
                    '1':['奥特曼玩偶',2.0],
                    '2':['高达',10.0],
                    '3':['钢铁侠1比1外套',100.0]
                }
                #账号登入
                #生成账号字典
                user_dict = dict()
                import os
                if not os.path.exists('用户注册信息.txt'):
                    with open('用户注册信息.txt', 'x', encoding='utf8') as f:
                        pass
                with open('用户注册信息.txt','r',encoding='utf8') as fr:
                    user_info = fr.read()
                user_info_list = user_info.split('|')[:-1]
                for user_pwd in user_info_list:
                    user_pwd = user_pwd.split(':')
                    user_name = user_pwd[0]
                    user_pwd = user_pwd[1]
                    user_dict[user_name] = user_pwd  #注册时候用户名肯定不同所以不需要判断
    
                #开始登入,生成用户信息内容为密码和姓名都是字符串格式
                while count == 0:
                    name = input('输入账号名字(输入我忘了退出会员积分兑换)')
                    pwd = input('输入账号密码')
                    if name =='我忘了':
                        break
                    if name not in user_dict:
                        print('账号错误')
                    elif pwd != user_dict[name]:
                        print('密码错误')
                    else:
                        print('登入成功')
    
                        #打印他剩余积分以及奖品兑换信息
                        print('-'*50)
                        print('可兑换的奖品'.center(50,' '))
                        print(f"输入0礼品为{gift_dict['0'][0]}需要积分为{gift_dict['0'][1]}分
    "
                              f"输入1礼品为{gift_dict['1'][0]}需要积分为{gift_dict['0'][1]}分
    "
                              f"输入2礼品为{gift_dict['2'][0]}需要积分为{gift_dict['2'][1]}分
    "
                              f"输入3礼品为{gift_dict['3'][0]}需要积分为{gift_dict['3'][1]}分
    "
                              f"退出输入X或者x")
                        # print(gift_dict)
                        # print(new_menber_integral_dict)
                        print('-' * 50)
    
                        #选择礼物
                        while True:
                            gift_choise = input('请输入你要兑换的礼物').strip()
                            if new_menber_integral_dict == dict():
                                print('你的积分为0分')
                                return
                            elif gift_choise not in gift_dict:
                                print('请在0,1,2,3,X,x中选择输入')
                                continue
                            else:
                                if gift_choise == 'X' or gift_choise == 'x':
                                    break
                                if name not in new_menber_integral_dict:
                                    print('你的账号没有积分')
                                    break
                                if gift_dict[gift_choise][1] > new_menber_integral_dict[name]:
                                    print(f'积分不足
    '
                                          f'您的剩余积分:{new_menber_integral_dict[name]}分')
                                else:
                                    print(f"恭喜你获得{gift_dict[gift_choise][0]}
    "
                                          f"剩余积分{new_menber_integral_dict[name]-gift_dict[gift_choise][1]}
    ")
                                    #将内容剩余积分进行统计
                                    with open('会员积分剩余积分.txt','a',encoding='utf8') as fw:
                                        fw.write(f'{name}:-{gift_dict[gift_choise][1]}|')
                                    count = 1
                                    break
            return gift
    
    
    print("-" * 50)
    print('欢迎来到杨大爷超市'.center(30,' '))
    print("-" * 50)
    x = 0
    while x == 0:
        print('-' * 50)
        your_chiose = input('输入0注册会员进入注册功能
    '
                            '输入1黑店我要走了
    '
                            '输入2直接进入购物
    '
                            '输入3会员积分查询
    '
                            '输入4会员积分兑换礼物
    '
                            '请输入:')
        print('-' * 50)
        if your_chiose not in ['0', '2', '1','3','4']:
            print('亲,输入0~4中数字')
            continue
        else:
            if your_chiose == '0':
                chiose(0)()
            elif your_chiose == '2':
                chiose(2)()
                chiose(1)()
            elif your_chiose == '3':
                chiose(3)()
            elif your_chiose == '4':
                chiose(4)()
            else:
                print('拜拜')
                x = 1
    
    
  • 相关阅读:
    Entity Framework笔记(二)
    C#依据word模版动态生成文档
    Entity Framework笔记(一)
    Ajax基础知识(二)
    Ajax基础知识(一)
    ASP.Net中使用XMLDataSource
    ubuntu安装jdk eclipse mysql等
    使用SQL Server Management Studio 创建数据库备份作业
    Visual Studion 2013 HTML 如何打开设计图
    Lodop6 以上打印控件使用,详参考自带说明文档,打印样式及文字大小要特殊设置一下
  • 原文地址:https://www.cnblogs.com/pythonywy/p/10966313.html
Copyright © 2020-2023  润新知