shangp = [["铅笔", 2],
["本子", 5], ["圆珠笔", 4], ["签字笔", 6], ["草稿", 3], ["橡皮擦", 2]]xuanzhe = {}huafei = 0while 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------------") breakelse:
print("请输入正确的编号!")