Basic Requirements:
User Portal
2. Press Q to exit
Realize:
1. Use the list and dic
2. If judgment while and for loop
3. Use documents to record product information and shopping records
" "Shopping Cart User Portal1, the product information exists in the document2, purchased goods, balance records, long-term preservation, that is, the first start is the need to enter the budget, and then take the rest of the money to buy things business entrance1, can add goods, modify the price of the product can be exited by Q" "Import os# define function # Client DEF customer (budgetfile,productfile): #显示或获取当前预算 budgetfd= Open (budgetfile,'R', encoding="Utf-8") Print ("you have now purchased:") Cartdata=budgetfd.readline () cartlist=[] Budget="' whilecartdata:cartlist.append (cartdata.split (" ")[0].strip ()) Print (cartdata.split (" ")[0]) Budget=cartdata.split (" ")[1] Cartdata=budgetfd.readline () Flag=Trueifbudget=="': whileFlag:budget=input ("Please enter your shopping budget:") ifBudget.isdigit (): Budget=int(budget) Flag=FalseElse: Print ("You must enter a Number. ") Continue Else: Print ("Your balance is:"+Budget) Budget=int(budget) budgetfd.close () Shopping=True whileshopping: # Displays the current product list first PRODUCTFD= Open (productfile,'R', encoding='Utf-8') Print ("Current Product List:") Data=productfd.readline () datadic= {} whileData:datadic[data.split (" ")[0].strip ()] = Data.split (" ")[1].strip () Print (data) data=productfd.readline () productfd.close () Choice=input ("Enter the name of the product you want to buy, and press Q to Exit:") ifchoice=='Q': Shopping=False; returnTrue elif Choiceinchdatadic:if int(datadic[choice]) <BUDGET:FD=open (budgetfile,'a', encoding='Utf-8') Budget-=int(datadic[choice]) Print ("Your balance is:"+str (budget)) cartlist.append (choice) fd.write (choice+' '+str (budget) +'\ n') fd.close () print ("you have now purchased:") forKeyinchCartlist:print (key)Else: Print ("not enough budget to buy. ") Else: Print ("the product does not exist. ") #商户端def boss (productfile): #打印目前商品列表 FD=open (productfile,'R', encoding='Utf-8') Fd.close () whileTrue: #先显示当前商品列表 fd.close () FD= Open (productfile,'r+', encoding='Utf-8') Print ("Current Product List:") Data=fd.readline () datadic= {} whileData:datadic[data.split (" ")[0].strip ()] = Data.split (" ")[1].strip () Print (data.split (" ")[0].strip (), Data.split (" ")[1].strip ()) Data=fd.readline () fd.close () Operation= Input ("Please enter the action you want to do: 1 means to increase the product, 2 to delete the item, 3 to modify the price of the commodity, Q to exit the Program:") ifoperation=='1': FD= Open (productfile,'a', encoding='Utf-8') string=input ("Please enter the items and prices you want to add, separated by a space:") Key=string. Split (" ")[0].strip () Value=string. Split (" ")[1].strip ()ifKey notinchDatadic:fd.write (string+'\ n') datadic[key]=valueElse: Print ("the item already exists and cannot be added") fd.close () elif Operation=='2': string= Input ("Please enter the name of the product to Delete:") FD=open (productfile,'W', encoding='Utf-8') if string inchDatadic:datadic.pop (string) Else: Print ("the item to be deleted does not exist. ") forKeyinchDatadic:fd.write (key+' '+ datadic[key] +'\ n') fd.close () elif Operation=='3': string=input ("Please enter the name and price of the product to be modified, separated by a space:") K=string. Split (" ")[0] v=string. Split (" ")[1] ifKinchdatadic:datadic[k]=vElse: Print ("the item to be modified does not exist. ") FD= Open (productfile,'W', encoding='Utf-8') forKeyinchDatadic:fd.write (key+' '+datadic[key]+'\ n') fd.close () elif Operation=='Q': Fd.close () flag=FalsereturnTrueElse: Print ("Please enter an accurate command. ") Operation= Input ("Please enter the action you want to do: 1 means to increase the product, 2 to delete the item, 3 to modify the price of the commodity, Q to exit the Program:") #脚本主程序exitflag=falseident=input ("are you a customer or a merchant:") whilenot exitflag:ifident=="Customer": Exitflag=customer ("Cartlist.txt","Productlist.txt") elif Ident=="Merchant": Exitflag=boss ("Productlist.txt") elif Ident=="Q": Exitflag=True Print ("exits the Program. ") Else: Print ("Please enter the exact word. ") Ident= Input ("are you a customer or a merchant:")
Python--shopping cart Optimization