购物车

Posted woz333333

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车相关的知识,希望对你有一定的参考价值。

购物车程序


需求:
(1)打印产品列表里的产品
(2)写一个循环不断问用户想买什么,用户选择一个商品编号,把对应的商品编号添加到购物车,打印购物车里的商品列表

代码:

 1 # __author__ = "wyb"
 2 # date: 2018/4/7
 3 # 需求:
 4 # (1)打印产品列表里的产品
 5 # (2)写一个循环不断问用户想买什么,用户选择一个商品编号,
 6 # 把对应的商品编号添加到购物车,打印购物车里的商品列表
 7 
 8 # 产品列表:
 9 products = [[IPhone8, 6888], [MacPro, 14688], [Coffee, 21], [book, 41], [Nike Shoes, 999]]
10 
11 # 购物车中的商品列表:
12 shopping_car = []
13 
14 while True:
15     # 打印商品列表:
16     print("商品列表".center(30, -))
17     for index, p in enumerate(products):
18         print("%s: %s   %s" % (index, p[0], p[1]))
19 
20     # 输入及输入处理及输出购物车里的商品列表:
21     # 输入:
22     choice = input("输入想购买的物品(输入q退出): ")
23     # 退出:
24     if choice == q or choice == Q:
25         # 输出购物车里的商品列表:
26         if shopping_car is []:
27             print("您没有购买任何商品")
28         else:
29             print("您购买了以下的商品".center(30, -))
30             for index, p in enumerate(shopping_car):
31                 print("%s: %s   %s" % (index, p[0], p[1]))
32         break
33     # 输入处理:
34     # 是数字:
35     elif choice.isdigit():
36         choice = int(choice)
37         if choice >= len(products) or choice < 0:
38             print("请输入正确的序号!")
39             continue
40     # 不是数字:
41     else:
42         print("请输入数字序号!")
43         continue
44 
45     # 向购物车中添加商品:
46     shopping_car.append(products[choice])
47     print("Add product %s into the shopping car" % (products[choice]))

 

以上是关于购物车的主要内容,如果未能解决你的问题,请参考以下文章

Magento:将静态块添加到结帐购物车

片段必须在创建之前调用寄存器 registerForActivityResult()

将对象列表从片段传递到 recyclerView 适配器

前端面试题-

如何使用实时数据从 RecyclerView 中删除项目?

微信小程序代码片段