products = [['phone1',100],['phone2',200],['phone3',300],['phone4',500],['phone5',500],['phone6',600]] shopping_cart = [] run_flag = True #标志位 while run_flag: print("---------商品列表----------") for index, p in enumerate(products): print("%s. %s %s" % (index, p[0], p[1])) # choice = int(input("输入想购买的商品编号:")) #input输入的都为字符串、所以要加上int choice = input("输入想购买的商品编号:") #input输入的都为字符串、所以要加上int if choice.isdigit(): choice = int(choice) if choice >= 0 and choice < len(products): shopping_cart.append(products[choice]) print("Added product %s into shopping cart. " %(products[choice])) else: print("商品不存在") elif choice == 'q': if len(shopping_cart) >0: print("---------您购买的产品为-----------") for index, p in enumerate(shopping_cart): print("%s. %s %s" % (index, p[0], p[1])) # break run_flag = False else: print("请输入数字")