5.22

shangp = [["iphone", 5888],
["coffee", 30],
["bike", 299],
["vivo x9", 2499],
["cake", 40],
["book", 99]]
xuanzhe = {}
huafei = 0
while True:
choice = input("输入你所选择的商品编号>>:").strip()
if choice.isdigit():
choice = int(choice)
if choice < len(shangp) and choice >= 0:
product = shangp[choice]
if product[0] in xuanzhe:
xuanzhe[product[0]][1] += 1
else:
xuanzhe[product[0]] = [product[1], 1]
print("目前你的购物车", xuanzhe)
elif choice == "buy":
print("---------你购买的商品如下---------")
print("id", "\t", "商品", "\t", "数量", "\t", "单价", "\t", "总价")
id_counter = 1
for key in xuanzhe:
print(id_counter, "\t",
key, "\t",
xuanzhe[key][1], "\t\t",
xuanzhe[key][0], "\t",
xuanzhe[key][1] * xuanzhe[key][0])
id_counter += 1
huafei+= xuanzhe[key][1] * xuanzhe[key][0]
print("总计价格为", huafei)
print("------------end------------")
break

else:
print("请输入正确的编号!")

猜你喜欢

转载自www.cnblogs.com/Ab889900/p/10904426.html