def like():
i = 1 # 为了通过修改i 退出多重循环
allChoice = []
while (i):
for num in range(len(shop)): # 打印商品列表
print(str(shop[num][0]).ljust(5), shop[num][1].ljust(20), str(shop[num][2]).ljust(10),
str(shop[num][3]).ljust(10))
choice = input("请输入要加入购物车的商品编号:")
choice = [int(it) for it in choice.split(‘ ‘)]
allChoice += choice # choice是单次选择的商品列表,allchoice是所有选择的商品列表
while (1):
total = 0
choiceSet = set(allChoice) # 转换成集合,便于不重复计数
for it in choiceSet:
print(shop[it - 1][0], shop[it - 1][1], shop[it - 1][2], ‘*‘, allChoice.count(it))
total += shop[it - 1][2] * allChoice.count(it)
print("总计:", total, )
print("---------------------------------\n"
"1:继续选购 2:整理购物车 Buy:结算\n")
option = input("请选择:")
if option == ‘1‘:
break
elif option == ‘2‘:
item_num = int(input("请输入要删除的商品:"))
allChoice.remove(item_num) # 每次只会删除一个元素
continue
if option == ‘Buy‘:
for it in choiceSet:
print(shop[it - 1][0], shop[it - 1][1], shop[it - 1][2], ‘*‘, allChoice.count(it))
total += shop[it - 1][2] * allChoice.count(it)
print("总计:", total, )
i = 0
break
else:
print("输入错误请重新输入!")
a=input("请输入“汽车”或“食物”:")
if a=="汽车":
shop = [[‘1‘, ‘奔驰‘, 150000, ‘汽车‘], [‘2‘, ‘宝马‘, 100000, ‘汽车‘], [‘3‘, ‘越野‘, 28000, ‘汽车‘],
[‘4‘, ‘沃尔沃‘, 12000, ‘汽车‘],
[‘5‘, ‘野马‘, 450000, ‘汽车‘], [‘6‘, ‘奥迪‘, 230000, ‘汽车‘], [‘7‘, ‘红旗‘, 100000, ‘汽车‘]]
like()
elif a=="食物":
shop = [[‘1‘, ‘苹果‘, 15, ‘水果‘], [‘2‘, ‘葡萄‘, 12, ‘水果‘], [‘3‘, ‘水蜜桃‘, 14, ‘水果‘],
[‘4‘, ‘香蕉‘, 3, ‘水果‘],
[‘5‘, ‘梨‘, 1, ‘水果‘],]
like()

原文:https://www.cnblogs.com/Locog/p/10903425.html