购物车:
1 product_list = [['iphone6s',5800],['mac book',9800],['coffee',32],['book',80],['bike',1500]] 2 shopping_cart = [] 3 money = input('please input your money:') 4 5 if money.isdigit(): 6 money=int(money) 7 while True: 8 for i, v in enumerate(product_list, 1): 9 print(i, v) 10 product_number = input('请输入产品编号[退出:q]:') 11 if product_number.isdigit(): 12 product_number=int(product_number) 13 if product_number>0 and product_number<6: 14 select_product = product_list[product_number-1] 15 if money >= select_product[1] : 16 money -= select_product[1] 17 shopping_cart.append(select_product) 18 print('已将%s加入购物车'%select_product[0],'您的余额为%s元钱'%money) 19 else: 20 print('余额不足,还剩%s'%money) 21 else: 22 print('序号不存在!') 23 elif product_number == 'q': 24 print('-------------您已购买如下商品-------------------') 25 for i in shopping_cart: 26 print(i) 27 print('您的余额为%s元钱'%money) 28 print('欢迎下次光临') 29 break 30 else: 31 print('invaild number!') 32 else: 33 print('invaild input!')