Python implementation of the shopping system example introduction

Source: Internet
Author: User
Here is a small series to bring you a Python implementation of the shopping system (sample explanation). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Requirements:

User Portal

1, the product information exists in the document
2, the purchase of goods, the balance record.

Merchant entrance

Can add goods, change the price of goods

Code:

Merchant Entrance:


# author:p J Jimport OSPs = "1 >>>>>> Modify Item 2 >>>>>> Add Item press Q for Exit Program ' # Open two files, f file for original accessor Product file, f_new file for modified product file F = open (' Commodit ', ' R ', encoding= ' utf-8 ') f_new = open (' Commodit_update ', ' w+ ', encoding= ' utf-8 ') File_list = F.readlines () # Print product information while true:productslist = [] # Data read from the product file is stored in the Productslist list for line in File_list:p Roductname = Line.strip (). Split () productname, Oldprice = Line.strip ("\ n"). Split () Productslist.append ([ProductName, Int (oldprice)]) Choose = Input ("%s Please select:"%ps) if Choose = = ' 1 ': For index, item in enumerate (productslist): Print (Index,   Item) Productindex = Input ("Enter the item number to modify the price:") if Productindex.isdigit (): productindex = Int (productindex) while True:    Print (' To modify product information: ', productslist[productindex]) Price = input ("Please enter the price to be modified:") if Price.isdigit (): Prices = Int. Productslist[productindex][1]=price Break Else:print ("Please enter the price correctly! ") Continue #已经修改好的商品列表循环写入f_new文件夹 in Productslist:insert_data = "%s%s"% (Products[0],products[1]) f_new.write (insert_data+ ' \ n ') print ("The price of the product has been modified! ") # Replace the original file F_new = open (' Commodit_update ', ' R ', encoding= ' utf-8 ') data = F_new.readlines () F = open (' Commodit ', ' w+ ' , encoding= ' Utf-8 ') for line on Data:f.write (line) F.close () f_new.close () #删除替换文件 os.remove (' commodit_update ') el If choose = = ' 2 ': # Add product f = open (' Commodit ', ' A + ', encoding= ' utf-8 ') Pricename = input ("Please enter product Name:") while True:price = Input ("Please enter commodity price:") if Price.isdigit (): F.writelines ('%s%s\n '% (pricename, price)) Break Else:print (' Input error please    Re-enter! ') Continue F.close () continue elif Choose = = ' Q ': Break else:print ("Input error Please reenter") continue

Buyer's entrance:


# author:p J jproductslist = []f = open (' Commodit ', ' R ', encoding= ' utf-8 ') for line in F:productname,price = Line.strip (' \ n ') ). Split () Productslist.append ((Productname,int (price)) print (productslist) shopping_list = []salary = input ("Please enter your cash : ") if Salary.isdigit (): salary = Int (salary) while True: # for item in Productslist: # print (Productslist.index (item), I TEM) for Index,item in Enumerate (productslist): Print (index,item) #判断用户要输入 user_choice = input ("Please select what to buy >>>:" If User_choice.isdigit (): user_choice = Int (user_choice) if User_choice < Len (productslist) and User_choice > = 0:p_item = Productslist[user_choice] If p_item[1] <= salary: #买得起 shopping_list.append (P_item) Salary -=P_ITEM[1] Print ("Add your balance to the%s shopping cart is \033[31;1M%S\033[0MRMB"% (p_item,salary)) else:print ("\033[32;1m your balance remains only [%s]rm b, also buy a wool \033[0m "%salary) else:print (" \033[41;1m you entered the product does not exist, please re-enter!\033[0m ") elif user_choice = ' Q ': print ("----sh Opping_list----") for p in SHOpping_list:print (p) print ("Your balance: \033[31;1M%S\033[0MRMB"%salary) #简单的余额记录 f = open (' Salary ', ' w+ ', encoding= ' utf- 8 ') F.writelines (str (salary)) F.close exit () else:print ("Error Options")

Operation Flow:


My directory:


1, create a new file, named Commodit product arrangement format as follows (you can change the name of the product or the price)

2, run the business entrance test function



We enter 1 to test the modified product first:




Enter 0 and modify the first item price to 400:



View Commodit file after exiting see product price has been modified




--------------------------------------------------

Test Add Item:



View Commodit File




Test Buyer Entry:



If you have money, get an iphone first.



60 more bags of furnace stone card bag



Press Q to exit Checkout! And there is a salary file to record the balance



The directory will have one more salary file at this time



You can see that the balance has been recorded.

Feelings:

Finished this shopping cart spent 2 days, in fact, is not all day, after all, still have to class, study. This time is mainly familiar with the operation of the document and some basic knowledge of the review, after writing can run out of the function is very happy. Because of the difficulties encountered in the middle, solved one after another problem, but through the Internet search and inquiry or solved. After writing the feeling is very low, after all, he knocked too little or more practice, this procedure is suitable for getting started or to learn the file operation of the pro to practice practiced hand. By the way, the test program is also a bug, but the impact is not particularly large, just do not change the price of the line, the problem I also thought how to solve, that is, the list is emptied, so that the data will not read 2 times, but also found that the second read data is not changed after the data, I think, the list has not been Clear function. Let's leave the question here. function has been achieved, but the writing is really low, and so on later mastered the new posture, back to change! Include the previous login and level three menu! If there is a beginner like me can study Alex Teacher's Python course, if there is great God see, and can patiently read, please the great God more pointing pointing brother!

Okay, life is short,use python!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.