电商购买作业

shangp = [["iphone", 5888],
["coffee", 30],
["bike", 299],
["vivo x9", 2499],

["oppo",2999],
          ["xiaomi",3999],

["cake", 400],
["book", 999]]
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("目前你的购物车", xuanze)
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("请输入正确的编号!")

 


 

posted @ 2019-05-22 10:57  林艺伟  阅读(178)  评论(0编辑  收藏  举报