列表练习题 简单购物车

列表练习题 简单购物车_第1张图片

 

个人代码,今后修改

products = [['Iphone',6888],['MacPro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]

#打印商品列表
print('--------------商品列表-----------')
for index,i in enumerate(products):
    print(index,i[0],i[1])
#购物车
shopping_list = [["占位",1,1]]
run_flag = True
while run_flag:
    id = input("请输入你要购买的商品(按q退出):")
    if id =='q' or id == 'Q':
        if len(shopping_list) > 1:
            print("结束服务!您购买的商品如下!")
            for i in shopping_list[1:]:
                print('商品名:',i[0],' 价格:',i[1],' 数量:',i[2])
        print("感谢购物 再见!")
        run_flag = False
    elif id.isdigit():
        id = int(id)
        if id <0 or id > len(products):
            print("您输入的商品号不正确,请重新输入!")
            continue
        else:
            print("您选择了商品是:",products[id][0],",价格是:",products[id][1],',已成功添加进购物车!')
            #查询商品是否存在购物车中
            x = 0
            for i in shopping_list:
                if products[id][0] in i[0]:
                    x = 1
                    i[2] += 1
                    break
                else:
                    x = 0
            if x == 0:
                shopping_list.append([products[id][0], products[id][1], 1])
    else:
        print("请输入正确的商品号!")
        continue

 

转载于:https://www.cnblogs.com/alexstraze/p/9241274.html

你可能感兴趣的:(列表练习题 简单购物车)