Problem needs:
1, start the program that allows the user to enter wages, and then print the Product List
2, allows users to purchase by product number
3, after the user selects a product, testing the balance is enough, enough to direct debit, is not enough to remind
4, may at any time withdraw, exit, print the purchase of goods and balances
1 __author__ = 'jcx' 2 3 product_list = [ 4 ('iphone11',5000), 5 ('macbook pro',9800), 6 ('Bike',800), 7 ('Coffee',31), 8 ('jcx\'s C++',60), 9 ] 10 11 shoping_list = [] 12 salary = input("Input your salary: ") 13 if salary.isdigit(): 14 salary = int(salary) 15 while True: 16 for index,item in enumerate(product_list): 17 print(index,item) 18 user_choice = input("选择要买的商品>>>: ") 19 if user_choice.isdigit(): 20 user_choice = int(user_choice) 21 if user_choice < len(product_list) and user_choice >= 0: 22 p_item = product_list[user_choice] 23 if p_item[1] <= salary: #买得起 24 shoping_list.append(p_item) 25 salary -= p_item[1] 26 print("Added %s into shopping cart, your current balance is \033[31;1m%s\033[0m" % (p_item,salary)) 27 else: 28 print("\033[41;1m你的余额只剩[%s]\033[0m" % salary) 29 else: 30 print("product number [%s] is not exist." % user_choice) 31 elif user_choice == 'q': 32 print("------ shoping list -------") 33 for p in shoping_list: 34 print(p) 35 print("You current balance: \033[31;1m%s\033[0m" % salary) 36 exit() 37 else: 38 print("invalid option")
Output:
. 1 the Input your the salary: 6000 2 0 ( ' iphone11 ' , 5000 ) . 3 . 1 ( ' MacBook Pro ' , 9800 ) . 4 2 ( ' Bike ' , 800 ) . 5 . 3 ( ' Coffee ' , 31 is ) . 6 . 4 ( " JCX apos C ++ " 60 ) 7 choose where to purchase >>> : 0 . 8 Added ( ' iphone11 ' , 5000) INTO Shopping Cart,your current balance is 1000 . 9 0 ( ' iphone11 ' , 5000 ) 10 . 1 ( ' MacBook Pro ' , 9800 ) . 11 2 ( ' Bike ' , 800 ) 12 is . 3 ( ' Coffee ' , 31 is ) 13 is . 4 ( " JCX apos C ++ " , 60 ) 14 to select SALE goods >>>:. 6 15 product Number [. 6] iS Not exist. 16 0 ( ' iphone11 ' , 5000 ) . 17 . 1 (' MacBook Pro ' , 9800 ) 18 is 2 ( ' Bike ' , 800 ) . 19 . 3 ( ' Coffee ' , 31 is ) 20 is . 4 ( " JCX apos C ++ " , 60 ) 21 is selected item to buy >>> : Q 22 is ---- - List Shoping ------- 23 is ( ' iphone11 ' , 5000 ) 24 by You Current Balance: 1000 25 26 is Process Finished Exit with code 0