列表練習題 簡單購物車

 

我的代碼,從此修改git

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
相關文章
相關標籤/搜索