1 salary = 5000 2 3 product_list = [ 4 ('mac',9000),('kindle',800),('tesla',900000),('python book',105),('bike',2000), 5 6 ] 7 shopping_car = [] 8 saving = input('please input your money:') 9 if saving.isdigit(): #校验是否是数字 10 saving = int(saving) 11 while True: 12 for i,v in enumerate(product_list,1): #给商品加编号 13 # print(product_list.index(i)+1,i) 14 print(i,'>>>',v) 15 choice = input('选择购买商品编号[退出:q]:') 16 if choice.isdigit(): 17 choice=int(choice) 18 if choice>0 and choice<len(product_list): 19 20 p_item = product_list[choice-1] 21 if p_item[1]<saving: 22 saving -= p_item[1] 23 shopping_car.append(p_item) 24 25 else: 26 print('余额不足,还剩%s' %saving) 27 28 29 print(p_item) 30 else: 31 print('编码不存在') 32 33 elif choice == 'q': 34 print('------您已经购买如下商品------') 35 for n in shopping_car: 36 print(n) 37 38 print('您还剩%s元'%saving) 39 40 break 41 else: 42 print('invalid input')