Read Me: following the implementation of the last simple shopping cart, it has been upgraded and optimized once again, and now it has the following functions:
1. There are customer operation and business operation, to realize that customers can buy things. When the amount is insufficient, press q to exit and print the shopping cart list
2. Businesses can add operations and modify commodity price operations
3. All the product information is stored in the product.txt file, for example, Watch: 1000, which exists in this form.
4. To run this program, you need to have the product information in the txt file, as shown above. Just follow the prompts
Mind mapping:
The code is as follows:
1 #!/usr/bin/env python 2 #-*- Coding:utf-8 -*- 3 # Author:Eric.Shen 4 #2018.02.06 5 #path python3.5 6 #Optimized shopping cart 7 #User access: 8 #1.Store the information of the goods in the file 9 #2.Purchased goods, balance record 10 #Merchant entrance: 11 #1.Can add item 2.Modify commodity price 12 #Store item list 13 import fileinput 14 product_list = [] 15 f = open("product.txt","r")#Open file 16 for line in f.readlines(): 17 line =line.strip()#Remove the last line break 18 index,item = line.split(":")#Get the front and back data by colon Division 19 product_list.append((index,item))#Data added 20 f.close() 21 22 def print_product_list(): 23 for index,item in enumerate(product_list): 24 print(index,item) 25 #User entrance 26 #User shopping 27 def user_shopping(): 28 salary = input("Please enter your salary:") 29 print_product_list() 30 if salary.isdigit(): 31 salary = int(salary) 32 shopping_list = []#Store user cart list 33 while True: 34 option = input("Buy whichever you like(Corresponding label): ") 35 if option.isdigit(): 36 option = int(option) 37 if option >= 0 and option <= len(product_list): 38 p_item = product_list[option]#Products selected by users 39 #print(product_list) 40 #print(p_item[1]) 41 c_num = int(p_item[1]) 42 if salary >= c_num: 43 shopping_list.append(p_item) 44 salary -= c_num 45 print("Successfully added shopping cart,Your balance still has%s"%(salary)) 46 else: 47 print("You don't have enough money left%s element"%(salary)) 48 else: 49 print("Input error, please input again!") 50 elif option == "q": 51 print("----------------Shopping list---------------") 52 for s_list in shopping_list: 53 print(s_list) 54 print("Your balance is%s"%(salary)) 55 print("..........exit.........") 56 exit() 57 else: 58 print("Invalid input") 59 60 #Merchant entrance 61 #Merchants add products 62 def add_product(): 63 name_of_product = input("Please enter the product name you want to add:") 64 price_of_product = input("Please enter the price of the item you want to add:") 65 f = open("product.txt","a") 66 f.write(str("\n"+name_of_product)+": %s"%(price_of_product)) 67 f.close() 68 print("Added successfully!\nexit----------") 69 70 71 72 #Modify commodity price 73 def change_price(): 74 print_product_list()#Print product list 75 choice = input("Please enter your choice:") 76 #name_of_change = input("Please enter the product name you want to change") 77 price_of_change = input("Please enter the price you want to change:") 78 if choice.isdigit(): 79 choice = int(choice) 80 if choice >=0 and choice <= len(product_list): 81 p_item = product_list[choice]#Selected products 82 #c_num = int(p_item[1])#Convert to int type 83 for line in fileinput.input("product.txt",inplace = "%s"%(choice)):#Modify the entered selection line 84 line = line.replace("%s"%(p_item[1]),"%s"%(price_of_change)).strip() 85 print(line) 86 exit("Modification succeeded!") 87 else: 88 print("Invalid input") 89 else: 90 if choice == "q": 91 exit("Sign out") 92 93 94 95 96 97 98 99 100 101 102 103 104 105 if __name__ == "__main__": 106 print("--------------------------" 107 "--------------------------" 108 "\n" 109 " Welcome to the shopping menu " 110 "\n" 111 "\n" 112 "Merchants please press b,Users please press c\n" 113 "--------------------------" 114 "--------------------------") 115 c_num = input("Please enter your choice:")#User selection 116 if c_num == "b": 117 print("--------------------------" 118 "--------------------------" 119 "\n" 120 " Welcome to the merchant interface " 121 "\n" 122 "\n" 123 "To add a product, press a,Please press c\n" 124 "--------------------------" 125 "--------------------------") 126 c_num2 = input("Please enter your choice:") 127 if c_num2 == "a": 128 #Realize the function of adding goods 129 add_product() 130 if c_num2 == "c": 131 #Realize the function of commodity price modification 132 change_price() 133 else: 134 print("Wrong input!") 135 if c_num == "c": 136 print("--------------------------" 137 "--------------------------" 138 "\n" 139 " Welcome to the user interface " 140 "\n" 141 "\n" 142 143 "--------------------------" 144 "--------------------------") 145 #Shopping function 146 user_shopping() 147 else: 148 print("Input error program exit!")
End: if there is any mistake, please point it out. Welcome to disturb-_-