简单购物车模拟

Posted notebook3013

tags:

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

 1 goods = [{"name": "电脑", "price": 1999},
 2          {"name": "鼠标", "price": 10},
 3          {"name": "游艇", "price": 20},
 4          {"name": "美女", "price": 998}]
 5 lst = [[电脑, 1999], [鼠标, 10], [游艇, 20], [美女, 998]]
 6 lst1 = [0,0,0,0]
 7 for i in range(len(goods)):
 8     print("{2},,{0},,{1}".format(goods[i]["name"], goods[i]["price"], i + 1))
 9 
10 def buy():
11     global msg1
12     while 1:
13         msg2 = input("请选择你要购买的商品序号:")
14 
15         if msg2.isdigit() and 0 < int(msg2) < len(goods) + 1:
16             #lst.append([goods[int(msg2) - 1]["name"], goods[int(msg2) - 1]["price"]])
17             lst1[int(msg2) - 1] +=1
18             if msg1 > 0:
19                 msg1 -= goods[int(msg2) - 1]["price"]
20             else:
21                 print("您的购物车余额不足!!")
22                 continue
23 
24         elif msg2.upper() == "Q":
25             #print("{0},,{1}".format(goods[int(msg)-1]["name"],goods[int(msg)-1]["price"]))
26             j = 0
27             for i in lst1:
28 
29                 print("您的购物车中有{0},{1}个!".format(lst[j],i))
30                 j += 1
31             print("您的购物车余额为:{}".format(msg1))
32             break
33         else:
34             print("你的输入有误!!")
35 
36 
37 def look():
38     while 1:
39         msg = input("请输入想要查看的商品序号输入Q去购买:")
40         if msg.isdigit() and 0 < int(msg) < len(goods) + 1:
41             print("{0},,{1}".format(goods[int(msg) - 1]["name"], goods[int(msg) - 1]["price"]))
42         elif msg.upper() == "Q":
43 
44             buy()
45             break
46         else:
47             print("您输入有误,,请重新输入!!")
48 
49 
50 while 1:
51     global msg1
52     msg1 = input("请先定义您的钱包余额!!")
53     if msg1.isdigit():
54         msg1 = int(msg1)
55 
56         look()
57         break
58     else:
59         print("请重新输入!")

 

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

模拟简单添加到购物车功能

vue2购物车ch1-(安装依赖简单配置 axios获取api的模拟数据)

JS代码实现商品价钱总和(模拟购物车功能)

超级简单的vue课程设计电商购物系统,没有数据库文件,通过模拟数据操作

jquery模拟淘宝购物车

python 之模拟购物车