python学习day05练习--购物车

"""
需求:
①客户输入目前拥有资金
②跳出商品列表
③客户选择商铺目录中的商品
④根据客户手头资金以及商品价格判断客户是否可以购买
⑤可以购买则添加至购物车,并计算出客户剩余资金
⑥不可以购买则提示客户还缺多少资金
⑦客户结束购物后提示客户选择了哪些商品以及对应商品的价格,并且计算出总价格以及客户剩余资金
"""


goods_list = [
    ["bike", 1200],
    ["pen ", 845],  # pen后面+一个空格,是因为\t 是补全(8-前面字符的位数%8)的距离,也就是说前面有1个字符那么在1个字符后输出一个\t,则\t的长度为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 ----------
        序号\t\t\t商品名称\t\t\t价格
        ''')
        for index_goods in range(goods_list.index(goods_list[-1])+1):
            print('''
        %d\t\t\t%s\t\t\t%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 -------
                序号\t\t商品名称\t\t价格
                ''')
                for index_goods in range(goods_choice.index(goods_choice[-1]) + 1):
                    print('''
                %d\t\t%s\t\t%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("欢迎下次光临")

猜你喜欢

转载自www.cnblogs.com/igeniuswwh/p/11254733.html