Python day2
1.购物车
1 shangpin_list = [ 2 ('Iphone',5800), 3 ('Tv',3500), 4 ('Book',65), 5 ('Bike',980), 6 ('Coffee',32), 7 ('key',150) 8 ] 9 shopping_list = [] 10 salary = input('请输入您的工资:') 11 if salary.isdigit():#如果输入的字符串为数字格式 12 salary = int(salary) 13 while True: 14 ''' 15 for i in enumerate(shangpin_list): 16 print(i) 17 break 18 ''' 19 for index,item in enumerate(shangpin_list):#获取shangping_list列表的索引下标和对应的数据 20 print(index,item) 21 user_choice = input('请输入要买商品的编号:') 22 if user_choice.isdigit():#如果输入的字符串为数字格式 23 user_choice = int(user_choice) 24 if user_choice < len(shangpin_list) and user_choice >= 0: 25 p_item = shangpin_list[user_choice] 26 if p_item[1] <= salary: #买得起 27 shopping_list.append(p_item) 28 salary = salary - p_item[1] 29 #print('购买成功,您花费和剩余工资为:', p_item[1],salary) 30 print('购买 %s 成功,您工资还剩 %s 元:' %(p_item,salary)) 31 else: 32 print('你的工资只剩 %s 了,还买个锤子呀!'%salary) 33 else: 34 print('商品[%s]不存在!'%user_choice) 35 elif user_choice == 'q': 36 print("用户退出") 37 print('----------------shopping list---------------') 38 for p in shopping_list: 39 print(p) 40 print('您的工资还剩 %s 元'%salary) 41 exit() 42 43 else: 44 print('错误选项') 45 else: 46 print('您输入的工资[%s]不是数字'%salary) 47