5.22

shangp = [["苹果", 12],
["香蕉", 5.8],
["梨", 6],
["橙", 14],
["葡萄", 20],
["牛油果", 60]]
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("请输入正确的编号!")

posted on 2019-05-22 11:30  zf0  阅读(103)  评论(0编辑  收藏  举报