购物车
products = [['iphone8',6888],['Macpro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]] # print('---------------------商品列表------------------') # for index,i in enumerate(products): # print('%s. %s %s' %(index,i[0],i[1])) shopping_cart = [] # 购物车 run_flag = True # 使用标志位退出while循环 while run_flag : print('---------------------商品列表------------------') for index, i in enumerate(products): print('%s. %s %s' % (index, i[0], i[1])) choice = (input("请输入想要购买商品的编号:")) # 输入购买商品编号 input 取值都是字符串 if choice.isdigit(): choice = int (choice) # 改成数字 if choice >= 0 and choice < len(products): # 判断输入值在商品编码范围内 shopping_cart.append(products[choice]) # 加入购物车 print('Added products %s into shopping cart. '%(products[choice])) else : print('商品不存在') elif choice == 'q' : if len(shopping_cart) > 0: # 判断购物车商品是否为空 print('------------------你已购买以下商品:-------------------') for index,i in enumerate(shopping_cart): # index 和 i 可以重复使用,占位 print('%s. %s %s' % (index, i[0], i[1])) # break run_flag = False
第一次练习 购物车作业