day3-购物车小程序
1.要求
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户genuine商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒还有多少
- 可随时退出,退出时,打印已购买商品和余额
code:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #Author:Daniel 2 # -*- coding: utf-8 -*- 3 #time:2017.10.26-10:10 4 Commodity = [ 5 ('IPhone',6800), 6 ('Samsung',7000), 7 ('Huwei',10000), 8 ('Mi',11000), 9 ('vivo',12000), 10 ('NOKIA',15000), 11 ] 12 shopping_cart = [] 13 salary = input('Please enter your salary:') 14 if salary.isdigit(): 15 salary = int(salary) 16 while True: 17 for index,Commodity_list in enumerate(Commodity): 18 print(index,Commodity_list) 19 user_choice = input('Please enter the commodity number') 20 if user_choice.isdigit(): 21 user_choice = int(user_choice) 22 if user_choice < len(Commodity) and user_choice >= 0: 23 Phone = Commodity[user_choice] 24 if Phone[1] <= salary: 25 shopping_cart.append(Phone) 26 salary -= Phone[1] 27 print('\033[31mThe %s has joined the shopping cart.Your balance is left %s\033[0m' % (Phone,salary)) 28 else: 29 print('Your balance is left %s Unable to buy' % salary) 30 else: 31 print('The input commodity number does not.Please re-enter') 32 elif user_choice == 'quit': 33 print('------------------- shopping list -------------------') 34 for shopping_cart_list in shopping_cart: 35 print(shopping_cart_list) 36 exit() 37 else: 38 print('Input error,Please re-enter') 39 else: 40 print('The input error program has exited. Please re-enter it')