Shopping Cart optimization,
Finally, I finally climbed out after countless jumps over the past few days. Corrected all the problems.
The program is a little complicated, but it is already very happy for me. After all, it is not a long time to learn, and it is still helpful for new users. I will certainly work harder and harder, study hard, and cheer up!
1 _ Author _ = "Zhang Peng" 2 product_list = [3 ('iphone ', 5888), 4 ('laptop', 12888), 5 ('Watch ', 10600), 6 ('TV', 3888), 7 ('book', 58), 8] 9 10 id_list = ["seller", "user", "shangjia ", "yonghu"] 11 id_shopping = input ("Please input your ID: \ n") 12 13 exit_flag = False 14 15 15 if id_shopping = "shangjia ": 16 choice = input ("do you want to add or modify a product? Add product input '1', modify product input '2', and exit the input 'q': \ n ") 17 if choice =" 1 ": 18 while not exit_flag: 19 name = input ("enter the name of the product you want to add: \ n") 20 name2 = input ("Enter the price of the product you want to add: \ n ") 21 if name = "q" or name2 = "q": 22 exit_flag = True 23 else: 24 product_list.append (name, name2 ),) 25 26 elif choice = "2": 27 while not exit_flag: 28 name3 = input ("enter the name of the product you want to change the price: \ n ") 29 name4 = input ("Enter the price you want to modify: \ n") 30 31 if name3 = "q" or n Ame4 = "q": 32 exit_flag = True 33 else: 34 a = product_list.index (name3) 35 product_list.remove (name3) 36 product_list.index (name3, name4) 37 38 else: 39 if choice = "q": 40 exit_flag = True 41 print (product_list) 42 43 44 elif id_shopping = "yonghu ": 45 shopping_list = [] 46 with open ("shopping_list.txt", "r", encoding = "utf8") as f_shop1: 47 print ("your shopping cart already has :") 48 print (f_shop1.read () 49 Open ("salary.txt", "r", encoding = "utf8") as f_salary1: 50 salary = f_salary1.read () 51 print (salary) 52 if salary. isdigit (): # isdigit is used to determine whether the input is a number 53 salary = int (salary) 54 while True: 55 print ("we have a product: \ n ") 56 for index, item in enumerate (product_list): 57 print (index, item) 58 user_choice = input ("What else do you want to buy?: \ N ") 59 if user_choice.isdigit (): 60 user_choice = int (user_choice) 61 if user_choice <len (product_list) and user_choice> 0: 62 p_item = product_list [user_choice] 63 if p_item [1] <salary: #64 shopping_list.append (p_item) 65 salary-= p_item [1] 66 print ("Your purchased item has been added to the shopping cart") 67 else: 68 print ("no money, don't worry") 69 else: 70 print ("no product you want") 71 elif user_choice = 'q': 72 print ("---------- shopping list ----- ----- ") 73 for p in shopping_list: 74 print (p) 75 with open (" shopping_list.txt "," a ", encoding =" utf8 ") as f_shop2: 76 f_shop2.write (str (p) 77 print ("You still have", salary) 78 with open ("salary.txt", "w", encoding = "utf8") as f_salary2: 79 f_salary2.write (str (salary) 80 exit () 81 82 else: 83 print ("input error") 84 85 else: 86 shopping_list = [] 87 salary = input ("Enter your monthly salary: \ n") 88 if salary. isdigit (): # isdigit is Judge whether the input is a number 89 salary = int (salary) 90 while True: 91 for index, item in enumerate (product_list): 92 print (index, item) 93 user_choice = input ("what do you want to buy?: \ N ") 94 if user_choice.isdigit (): 95 user_choice = int (user_choice) 96 if user_choice <len (product_list) and user_choice> 0: 97 p_item = product_list [user_choice] 98 if p_item [1] <salary: #99 shopping_list.append (p_item) 100 salary-= p_item [1] 101 print ("Your purchased item has been added to the shopping cart") 102 else: 103 print ("no money, don't worry") 104 else: 105 print ("no product you want") 106 elif user_choice = 'q': 107 print ("---------- shopping list ----------") 108 for p in shopping_list: 109 print (p) 110 with open ("shopping_list.txt", "a", encoding = "utf8") as f_shop3: 111 f_shop3.write (str (p )) 112 print ("remaining", salary) 113 with open ("salary.txt", "w", encoding = "utf8") as f_salary3: 114 f_salary3.write (str (salary )) 115 exit () 116 117 else: 118 print ("input error ")
I hope I can get some comments. This is my greatest learning motivation! Thank you!