The following optimizations were made for the last program:
1. The entire program is divided into two interfaces, respectively, the merchant can be a user
2. Merchants can add items, modify prices
3. The product information is stored in the document, the balance and the purchased goods are also in the document
1products_list=[]2shopping_list=[]3price_list=[]4 whileTrue:5Role=input ("Please choose your identity to enter the system 1.user| 2.ower:")6 ifrole=='1':7Print ("Welcome to the system using user identity")8With open ("Code_file/product_info",'R') ash:9f=Len (H.readlines ())TenWith open ("Code_file/product_info",'R') asu: One forIinchrange (f): ASingle_produce=u.readline (). Strip ('\ n') - products_list.append (single_produce) -With open ("Code_file/price_info",'R') asm: the forIinchrange (f): -Single_price=int(M.readline (). Strip ('\ n')) - price_list.append (Single_price) -Salary=input ("Input your Salary:") + ifsalary.isdigit (): -salary=int(Salary) + whileTrue: A forIndex,iteminchEnumerate (products_list): at print (Index,item,price_list[index]) -Choose_num=input ("plese your choose ...:") - ifchoose_num.isdigit (): -choose_num=int(Choose_num) - ifChoose_num<len (products_list) and Choose_num >=0: - ifsalary>=Price_list[choose_num]: in shopping_list.append (Products_list[choose_num]) -p=Products_list[choose_num] tosalary-=Price_list[choose_num] +Print"added \033[31;1m%s\033[0m to your Shopcar...,your balance is \033[31;1m%s\033[0m"%(p,salary)) - Else: thePrint"your salary can not afford it ....") * Else: $Print"Now,your Choose is false ....")Panax NotoginsengElif choose_num=="Q": -With open ("Code_file/bought_info",'A +') asbought: the forBought_productsinchshopping_list: + bought.writelines (bought_products) ABought.writelines ('\ n') the print (bought_products) +With open ("Code_file/balance_info",'W') asBalance: - Balance.writelines (str (salary)) $Balance.writelines ('\ n') $Print"your balance is \033[31;1m%s\033[0m"%salary) - exit () -Elif choose_num=='b': the Break - Else:WuyiPrint"your input is False....,plese input agin ...") the -Elif role=='2': WuPrint ("you are welcome to enter the system using your business identity") - whileTrue: AboutPrint (" "operational information that can be done by the merchant $ 0. Add Item - 1. Modify the price - 2. Reading information about a product - " ") ANext=input ("Please enter your choice:") + ifnext.isdigit (): thenext=int(Next) - ifnext==0: $Join_product=input ("Please enter the added product name:") theJoin_price=input ("Please enter the price of the added product:") theWith open ("Code_file/product_info",'A +') aso: the o.writelines (join_product) theO.writelines ('\ n') -With open ("Code_file/price_info",'A +') asA: in a.writelines (Join_price) theA.writelines ('\ n') the Continue AboutElif next==1: theChange_name=input ("Please enter the name of the product to be modified:")+'\ n' theChange_price=input ("Please enter this product to change to more money:")+'\ n' theWith open ("Code_file/price_info",'r+') aso: +Read_price=O.readlines () - print (Read_price) theRead_price[products_list.index (change_name)] =Change_priceBayi print (Read_price) theWith open ("Code_file/price_info",'w+') asQ: the q.writelines (Read_price) -Elif next==2: -With open ("Code_file/product_info",'R') asB: theProducts_list =B.readlines () the forIndex,iteminchEnumerate (products_list): the print (Index,item) theElif next=='b': - Break theElif next=='Q': the exit () the Else:94Print"Input Error") theElif role=='Q': the exit () the Else:98Print"Input Exception")
Shopping Cart Program Optimization (python3.x)