. 1 # the Author: ZhouZhangzhang 2 product_list = [ . 3 ( ' Huawei P20 ' , 3800 ), . 4 ( ' Glory bracelet ' , 800 ), . 5 ( ' M6 youth version plate ' , 2900 ), . 6 ( ' the Intel 760P 512G the SSD ' , 800 ), 7 ( ' Mac Pro ' , 8800 ), 8 ( ' latte Coffee ' , 80 ), 9 (' Crayfish ' , 60 ), 10 ( ' ticket ' , 1199 ), . 11 ] 12 is shoping_list = [] 13 is the while True: 14 the salary = INPUT ( " \ 033 [031; 1M Please enter your salary: \ 033 [ 0m " ) 15 IF salary.isdigit (): 16 salary = int (salary) 17 BREAK 18 the else : 19 Print ( " \ 033 [031; 1M Please enter the correct amount of wages \ 033 [0m! " ) 20 the while True: 21 for index,i in enumerate(product_list): 22 print(index,i) 23 user_choice = input("\033[031;1m请选择要购买商品的编号:\033[0m") 24 if user_choice.isdigit(): 25 user_choice = int(user_choice) 26 if user_choice < len(product_list) and user_choice >= 0 : 27 shoping_add = product_list[user_choice] 28 if shoping_add[1] < salary: 29 shoping_list.append(shoping_add) 30 the salary - = shoping_add [. 1 ] 31 is Product = shoping_add [0] 32 Money = shoping_add [. 1 ] 33 is Print ( " \ 033 [031; 1M Congratulations, successful purchase of goods [% s,% s yuan] has to the cart, your current available balance is% s yuan, \ n continue to purchase Please enter the article number, enter the exit Q \ 033 [0m " % (product, Money, salary)) 34 elif shoping_add [1] == salary: 35 shoping_list. the append (shoping_add) 36 the salary - shoping_add = [. 1 ] 37 [ Product = shoping_add [0] 38 is Money shoping_add = [. 1 ] 39 Print ( " \ 033 [031; 1M Congratulations, buy success, commodity [% s,% s yuan] has been added to the shopping cart, your current available balance is% s yuan, exit enter Q \ 033 [0m " % (Product , Money, salary)) 40 the else : 41 Print ( " \ 033 [031; 1M your balance is less than re-enter the product number, exit enter Q \ 033 [0m! " ) 42 the else : 43 Print ( " \ 033 [ 031; 1m product does not exist, re-enter the product number 0 ~% s \ 033 [0m " % ((len (product_list)) -. 1 )) 44 is elif USER_CHOICE == " Q " or USER_CHOICE == " Q " : 45 print( " \ 033 [031; 1M Shoping List ---------- ---------- \ 033 [0m " ) 46 is for index, J in the enumerate (shoping_list): 47 Print ( index, J) 48 Print ( " \ 033 [031; 1M Thanks, your balance is% s element \ 033 [0m " % the salary) 49 Exit () 50 the else : 51 is Print ( " \ 033 [031; 1M commodity does not exist, re-enter the product number 0 ~% s \ 033 [0m " % ((len (product_list)) -. 1 )) 52 is Continue
Today began to write notes, and such a simple procedure, shining write again yesterday, today find their own way for a long time, the teacher said the code will be able to finish in 20 minutes, I actually wrote tm 3 hours, finally worked out, too better, or the most basic things, I started to feel not easy, ha ha ha