首页 > 代码库 > python 之模拟购物车

python 之模拟购物车

#全部小代码在此

product_list = [[Iphone7,5800],
                [Coffee,30],
                [疙瘩汤,10],
                [Python Book,99],
                [Bike,199],
                [ViVo X9,2499]
                ]

shopping_cart = []

salary = int(input("input your salary:"))

while True:
    menu = u‘‘‘
       ------- Good Shopping ---------
       \033[32;1m
       1.  输入序号买商品进入购物车
       2.  输入q打印购物清单退出
       \033[0m‘‘‘
    print(menu)
    index = 0
    for product in product_list:
        print(index,product)
        index +=1
    choice = input(">>").strip()
    if choice.isdigit(): #判断是否为数字
        choice = int(choice)
        if choice >= 0 and choice < len(product_list):#商品存在
            product = product_list[choice]#取到商品
            if product[1] <= salary:#判断能否买得起
                shopping_cart.append(product)#加入购物车
                salary -= product[1]#扣钱
                print("added product "+ product[0] + "into shopping cart,your current balance"+ str(salary))
                print("现在的余额为 %d 元" % salary)
                #print("jia ge shi " + product[haproxy.conf] + "yuan")
            else:
                print("买不起,穷逼!产品价格是"+str(product)[1]+"你还差钱"+str(product[1]-salary)+"")
        else:
            print("商品不存在")
    elif choice == "q":
        print("----已购买商品列表-----")
        print("id 商品 数量 单价 总价")
        for i in shopping_cart:
            print(i)

        print("您的余额为:",salary)
        print("------end------")
        break
    else:
        print("无此选项!")

技术分享

技术分享

技术分享

 

python 之模拟购物车