zoukankan      html  css  js  c++  java
  • python学习day05练习--购物车

    """
    需求:
    ①客户输入目前拥有资金
    ②跳出商品列表
    ③客户选择商铺目录中的商品
    ④根据客户手头资金以及商品价格判断客户是否可以购买
    ⑤可以购买则添加至购物车,并计算出客户剩余资金
    ⑥不可以购买则提示客户还缺多少资金
    ⑦客户结束购物后提示客户选择了哪些商品以及对应商品的价格,并且计算出总价格以及客户剩余资金
    """
    
    
    goods_list = [
        ["bike", 1200],
        ["pen ", 845],  # pen后面+一个空格,是因为	 是补全(8-前面字符的位数%8)的距离,也就是说前面有1个字符那么在1个字符后输出一个	,则	的长度为7个字符长度
        ["pants", 800],
        ["shoe", 1399],
        ["iphone", 9100]
    ]
    goods_choice = []
    
    input_correct_of_salary = False
    customer_choice_continue = False
    
    while not input_correct_of_salary:
        salary = input("剩余资金:")
        if salary.isdigit():
            salary = int(salary)
            print('''
            --------- Goods List ----------
            序号			商品名称			价格
            ''')
            for index_goods in range(goods_list.index(goods_list[-1])+1):
                print('''
            %d			%s			%d
                ''' % (index_goods+1, goods_list[index_goods][0], goods_list[index_goods][1]))
            print('''
            ------ 结束购物请输入quit -------
            ''')
    
            while not customer_choice_continue:
                customer_choice = input("退出或输入商铺编号:")  # 暂不考虑客户输入是否符合要求,默认输入正确
                if customer_choice == "quit":
                    print('''
                    你购买了以下商品
                    -------- Good Choice -------
                    序号		商品名称		价格
                    ''')
                    for index_goods in range(goods_choice.index(goods_choice[-1]) + 1):
                        print('''
                    %d		%s		%d
                        ''' % (index_goods + 1, goods_choice[index_goods][0], goods_choice[index_goods][1]))
                    break
    
                elif salary >= goods_list[int(customer_choice) - 1][1]:
                    goods_choice.append(goods_list[int(customer_choice)-1])
                    salary = salary - goods_list[int(customer_choice)-1][1]
                    print("已将商品%s添加至购物车,目前剩余资金%d" % (goods_list[int(customer_choice)-1][0], salary))
    
    
                else:
                    print("你目前资金离购买该商品还差%d" % (goods_list[int(customer_choice)-1][1]-salary))
                    # 如果客户资金比任何商品价格都小,则提示客户的资金已不足以购买任何商品
    
    
            input_correct_of_salary = True
        else:
            print("请正确输入")
        print("欢迎下次光临")
  • 相关阅读:
    gray-code——找规律
    [LeetCode] Decode Ways 解码方法个数、动态规划
    操作系统之面试常考(转)
    国内90%以上的 iOS 开发者,对 APNs 的认识都是错的
    vim配置为IDE环境(超详细,极力推荐 git)
    curl的使用(from 阮一峰)
    图片鉴黄服务提供商
    转: 【理念篇】关于数据驱动运维的几点认识
    业务调度链的染色数据上报和关联
    ITIL的考核管理体系
  • 原文地址:https://www.cnblogs.com/igeniuswwh/p/11254733.html
Copyright © 2011-2022 走看看