zoukankan      html  css  js  c++  java
  • day14 三百行的购物车代码(强行三百行)

    三百行的购物车代码

    强行三百航,pycharm整理后实为285行!!!

    shopping_car_dict = dict()  # 存储购买商品
    user_info_dict = dict()  # 记录用户登陆信息
    goods_price_total = 0  # 总价
    
    
    def input_username_pwd():
        """输入用户名/密码"""
        username = input('请输入你的名字》》》')
        pwd = input('请输入你的密码》》》')
    
        return username, pwd
    
    
    def save_file_a(filename, content):
        """a模式保存文件"""
        with open(filename, 'a', encoding='utf8') as fa:
            fa.write(content)
    
        return True
    
    
    def save_file_w(filename, content):
        """w模式保存文件"""
        with open(filename, 'w', encoding='utf8') as fw:
            fw.write(content)
    
        return True
    
    
    def read_file(filename):
        """读取文件"""
        with open(filename, 'r', encoding='utf8') as fr:
            data_list = fr.readlines()
    
        return data_list
    
    
    def login_deco(func):
        def wrapper(*args, **kwargs):
            if not user_info_dict:
                login_res = login()
                if login_res:
                    res = func(*args, **kwargs)
                    return res
                else:
                    print('登陆失败,请重新登陆')
                    return
    
            res = func(*args, **kwargs)
            return res
    
        return wrapper
    
    
    def register():
        """注册"""
        print("33[1;31;40m欢迎使用注册功能33[0m")
    
        while True:
            username, pwd = input_username_pwd()
    
            # 判断用户名是否已经存在
            user_info_data_list = read_file('user_info.txt')
            for user_info in user_info_data_list:
                user_list = user_info.strip().split(':')
                username_file = user_list[0]
    
                if username_file == username:
                    print('用户名已经存在,请重新注册')
                    break
            else:
                save_file_a('user_info.txt', f'{username}:{pwd}:15000:0
    ')
                break
    
    
    def login():
        """登陆,登陆三次后退出"""
        print("33[1;31;40m欢迎使用登陆功能33[0m")
    
        count = 0
        while count < 3:
            username, pwd = input_username_pwd()
    
            # 判断用户是否存在
            user_info_data_list = read_file('user_info.txt')
            for user_info in user_info_data_list:
                # 姓名:密码:余额:锁
                user_list = user_info.strip().split(':')
                username_file = user_list[0]
                pwd_file = user_list[1]
                extra = int(user_list[2])
                lock_num = user_list[3]
                lock_num_int = int(lock_num)
    
                if username == username_file and pwd == pwd_file:
                    if lock_num_int == 0:
                        print('登陆成功')
                        # 存放用户信息
                        user_info_dict['username'] = username_file
                        user_info_dict['pwd'] = pwd_file
                        user_info_dict['extra'] = extra
                        user_info_dict['lock_num'] = lock_num
                        return True
                    else:
                        print('账户被冻结')
                        return False
            else:  # for循环没有被break/return后执行
                count += 1
                print('账户名/密码错误,请重新登陆')
    
    
    @login_deco
    def shopping():
        """购物"""
        print("33[1;31;40m欢迎使用购物功能33[0m")
    
        goods_dict = {
            '0': ('tesla', 100),
            '1': ('tiger', 1000),
            '2': ('nick全*写真集', float("inf")),
            '3': ('nick半*写真集', float("inf") / 2),
            '4': ('iphoneX', 10),
            '5': ('MacPro', 15),
            'q': '退出'
        }
    
        while True:
            for ind, goods in goods_dict.items():
                print(ind, goods)
    
            goods_choice = input('请选择你需要购买的商品号(输入q退出)》》》').strip()
    
            if goods_choice == 'q':
                break
    
            if goods_choice not in goods_dict:
                print('傻逼,眼瞎,滚走,讨厌~!')
                continue
    
            goods_num_choice = input('请输入你需要购买的商品数量(输入q退出)》》》').strip()
    
            if goods_num_choice == 'q':
                break
    
            if not goods_num_choice.isdigit():
                print('傻逼,你数学体育老师教的???')
                continue
    
            goods_num_choice_int = int(goods_num_choice)
    
            shopping_goods = goods_dict[goods_choice]
            print(f'你购买的{shopping_goods} {goods_num_choice_int}已经加入购物车,请进入购物车结账!')
    
            if not shopping_car_dict.get(shopping_goods):
                shopping_car_dict[shopping_goods] = goods_num_choice_int
            else:
                shopping_car_dict[shopping_goods] += goods_num_choice_int
    
    
    @login_deco
    def shopping_car():
        """购物车"""
        print("33[1;31;40m欢迎使用购物车功能33[0m")
        global goods_price_total
    
        while True:
            # 打印购物车信息+计算总价
            print('你的购物车有如下产品:')
            for shopping_goods, goods_num in shopping_car_dict.items():
                print(shopping_goods, goods_num)
                goods_price_total += shopping_goods[1] * goods_num
    
            if goods_price_total > user_info_dict['extra']:
                print('穷逼,余额不足,无法购买,平台无法充值,请修改or请滚蛋!!!')
    
                # 修改购物车
                while True:
                    for ind, goods in enumerate(shopping_car_dict):
                        print(ind, goods)
    
                    shopping_car_list = [goods for _, goods in enumerate(shopping_car_dict)]
                    modify_goods_ind = input('请选择你需要修改的商品编号》》》').strip()
                    modify_goods = shopping_car_list[int(modify_goods_ind)]
                    modify_goods_num = int(input('请选择你需要修改的商品数量》》》').strip())
                    shopping_car_dict[modify_goods] = modify_goods_num
    
                    break
            else:
                print('你可以去结账了')
                break
    
    
    @login_deco
    def check():
        """结账"""
        print("33[1;31;40m欢迎使用结账功能33[0m")
    
        if goods_price_total <= user_info_dict['extra']:
            user_info_dict['extra'] -= goods_price_total
            # 清空购物车
            global shopping_car_dict
            shopping_car_dict = dict()
            print(f'结账{goods_price_total}成功,你余额为{user_info_dict["extra"]}')
        else:
            print('余额不足,请前往购物车修改商品信息')
            shopping_car()
    
    
    @login_deco
    def lottery():  # 抽奖
        """抽奖"""
        print("33[1;31;40m欢迎使用抽奖功能33[0m")
        pass
    
    
    @login_deco
    def goods_back():
        """退货"""
        print("33[1;31;40m欢迎使用退货功能33[0m")
        pass
    
    
    @login_deco
    def coupon():
        """优惠券"""
        print("33[1;31;40m欢迎使用优惠券功能33[0m")
        pass
    
    
    @login_deco
    def shopping_history():
        """消费记录"""
        print("33[1;31;40m欢迎使用消费记录功能33[0m")
        pass
    
    
    '**************************************************'
    
    func_dict = {
        0: register,
        1: login,
        2: shopping,
        3: shopping_car,
        4: check,
        5: lottery,  # 抽奖
        6: goods_back,
        7: coupon,
        8: shopping_history,
    }
    
    func_msg = """
    0:注册,
    1:登录,
    2:购物,
    3:购物车,
    4:结账,
    5:抽奖
    6:退货,
    7:优惠券,
    8:消费记录,
    q:退出
    """
    
    while True:
        print(func_msg)
    
        func_choice = input('请输入你想要使用的功能(输入q退出)》》》').strip()
    
        # 退出
        if func_choice == 'q':
            break
    
        # 判断是否非法输入
        if not func_choice.isdigit():
            print('非法输入')
            continue
    
        # 判断是否非法输入
        func_choice_int = int(func_choice)
        if func_choice_int not in func_dict:
            print('非法输入')
            continue
    
        # 正常选择功能
        func_dict[func_choice_int]()
    
  • 相关阅读:
    对Cost (%CPU) 粗略的理解
    SQL AND &amp; OR 运算符
    [Nagios] Error: Template &#39;timman&#39; specified in contact definition could not be not found (c
    质因数分解
    细数人体器官仿生,还有哪些可开发的
    利用京东云擎架设免费Wordpress 博客(git方式)
    C++内存管理变革(6):通用型垃圾回收器
    二分查找法
    百度云存储教程---免费建立自己的静态网站
    paip.提升效率---filter map reduce 的java 函数式编程实现
  • 原文地址:https://www.cnblogs.com/wsxiaoyao/p/10976317.html
Copyright © 2011-2022 走看看