用列表增删改查实现购物车的添加、删除、结算功能。
用户可以选择查看不同商品分类,将商品加入购物车并选择商品数量(1-99);
可以查看购物车列表;
可以删除购物车中的商品;
可以点击结算购物车(退出程序)。
商品列表数据如下:
商品列表:
1. 食品
香辣鸡翅 19.9
肉松蛋黄卷 6.8
陕西红富士 29.9
2. 服饰
五分裤 49.9
polo衫 32.9
墨镜 150
拖鞋 20
3. 手机
iPhone13 5659
华为P50 4088
小米12 3199
vivoZ5 2199
----------以下是代码----------
# -*- coding = utf - 8 -*-
# @Time : 2022/5/4 10:12 AM
# @Author : kk
# @File : demo2.py
# @Software : PyCharm
p = "商品列表"
t = "购物车"
total = 0 #购物车总金额
exitp = "您已退出,感谢您的光临"
namelist = ["食品", "服饰", "手机"]
shoppinglists = []
trolley = []
shoppinglist1 = [["香辣鸡翅", 19.90], ["肉松蛋黄卷", 6.80], ["陕西红富士", 29.90]] #食品列表
shoppinglist2 = [["五分裤", 49.90], ["polo衫", 32.90], ["墨镜", 150.00], ["拖鞋", 20.00]]
shoppinglist3 = [["iPhone13", 5659.00], ["华为P50", 4088.00], ["小米12", 3199.00], ["vivoZ5", 2199.00]]
shoppinglists.append(shoppinglist1)
shoppinglists.append(shoppinglist2)
shoppinglists.append(shoppinglist3)
print("------%s------"%p)
count1 = 0
for list in shoppinglists :
count2 = 1
print("%dt%s"%(count1+1, namelist[count1]))
count1 += 1
for product in list :
print("t%dt%st%.2f"%(count2, product[0], product[1]))
count2 += 1
#以上是商品列表创建及打印代码段
choicel =input("请输入您想加购的商品类别编号n")
choicep =input("请输入您想加购的商品编号,退出请输入除商品编号数字之外的任意字符n")
while str.isdigit(choicel) == True and str.isdigit(choicep) == True:
choicel = int(choicel)
choicep = int(choicep)
if choicel == 1 or choicel == 2 or choicel == 3 :
if choicep >= 1 and choicep <= len(shoppinglists[choicel-1]):
add = []
num = input("您将加购的商品为%s,单价为%.2f,请输入您要加购的数量(1-99)n" % (shoppinglists[choicel - 1][choicep - 1][0], shoppinglists[choicel - 1][choicep - 1][1]))
num = int(num)
if num < 1 or num > 99:
print(exitp)
else:
add.append(shoppinglists[choicel - 1][choicep - 1][0])
add.append(int(shoppinglists[choicel - 1][choicep - 1][1]) * num)
trolley.append(add)
total += add[1]
print("------%s------"%t)
count3 = 0
for list in trolley:
print("%dt%st%.2f" % (count3 + 1, list[0], float(list[1])))
count3 += 1
print("总计金额:%.2f"%float(total))
choicel =input("请输入您想加购的商品类别编号n")
choicep = input("请输入您想加购的商品编号,结算清输入'OK',退出请输入其他任意字符n")
else:
print(exitp)
else :
if choicel =="OK" or choicep == "OK":
print("请您支付%.2f元,谢谢惠顾"%total)
else :
print(exitp)
----------以下是结果演示----------
Python实现购物车功能演示



