shop = [["iphone", 5888],
["1+", 2999], ["坚果pro", 1999], ["vivo x9", 2499], ["华为", 3999], ["小米", 1299]]getshop = {}money = 0while True: choice = input("输入你所选择的商品编号>>:").strip() if choice.isdigit(): choice = int(choice) if choice < len(shop) and choice >= 0: product = shop[choice] if product[0] in getshop: getshop[product[0]][1] += 1 else: getshop[product[0]] = [product[1], 1] print("目前你的购物车", getshop) elif choice == "buy": print("---------你购买的商品如下---------") print("id", "\t", "商品", "\t", "数量", "\t", "单价", "\t", "总价") id_counter = 1 for key in getshop: print(id_counter, "\t", key, "\t", getshop[key][1], "\t\t", getshop[key][0], "\t", getshop[key][1] * getshop[key][0]) id_counter += 1 money+= getshop[key][1] * getshop[key][0] print("总计价格为", money) print("------------end------------") breakelse:
print("请输入正确的编号!")