Python implements the Shopping System (example) and python Shopping System

Source: Internet
Author: User

Python implements the Shopping System (example) and python Shopping System

Requirements:

User Portal

1. The product information is stored in the file.
2. purchased items and balance records.

Merchant portal

You can add products and modify product prices.

Code:

Merchant portal:

# Author: p j Jimport osps = ''' 1 >>>>>>> modify product 2 >>>>>> add product by q As exit program ''' # Open two files, f file is the original product file, f_new file is the modified product file f = open ('commodit ', 'R', encoding = 'utf-8 ') f_new = open ('commodit _ Update', 'W + ', encoding = 'utf-8') file_list = f. readlines () # print the product information while True: productslist = [] # store the data read from the product file in the productslist list for line in file_list: productname = line. strip (). split () productname, oldprice = line. strip ("\ n "). sp Using () productslist. append ([productname, int (oldprice)]) choose = input ("% s select:" % ps) if choose = '1': for index, item in enumerate (productslist): print (index, item) productindex = input ("Enter the product number to change the price:") if productindex. isdigit (): productindex = int (productindex) while True: print ('item information to be modified: ', productslist [productindex]) price = input ("Enter the price to be modified: ") if price. isdigit (): price = int (price) productslis T [productindex] [1] = price break else: print ("Please enter the correct price! ") Continue # The modified product list is cyclically written to the f_new folder for products in productslist: insert_data =" % s "% (products [0], products [1]) f_new.write (insert_data + '\ n') print ("the product price 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 in data: f. write (line) f. close () f_new.close () # Delete the OS replacement file. remove ('commodit _ Update') elif choose = '2': # add product f = open ('commodit ', 'a +', encoding = 'utf-8 ') pricename = input ("Enter product name:") while True: price = input ("Enter product price:") if price. isdigit (): f. writelines ('% S \ n' % (pricename, price) break else: print ('input error. Please try again! ') Continue f. close () continue elif choose = 'q': break else: print ("input error please input again") continue

Buyer Portal:

# 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 ("Enter your cash:") if salary. isdigit (): salary = int (salary) while True: # for item in productslist: # print (productslist. index (item), item) for index, item in enumerate (productslist ): Print (index, item) # determine which user to enter user_choice = input ("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 % s to shopping cart your balance is \ 033 [31; 1 m % s \ 033 [0 mRMB" % (p_item, salary )) else: print ("\ 033 [32; 1 m your remaining balance is [% s] RMB, and you can buy a wool \ 0 33 [0 m "% salary) else: print (" \ 033 [41; 1 m the item you entered does not exist. Please enter it again! \ 033 [0 m ") elif user_choice = 'q': print (" ---- shopping_list ---- ") for p in shopping_list: print (p) print (" your balance: \ 033 [31; 1 m % s \ 033 [0 mRMB "% salary) # simple balance record f = open ('salary ', 'W + ', encoding = 'utf-8') f. writelines (str (salary) f. close exit () else: print ("error option ")

Procedure:


My directory:


1. Create a new file named commodit in the following format (you can change the product name or price on your own)

2. Run the merchant portal test function



We enter 1, first test and modify the product:




Enter 0 to change the price of the first item to 400:



View the commodit file and check that the product price has been modified.




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

Test adding product:



View the commodit File




Test buyer entry:



If you have money, you can start with an Iphone.



Another 60 packs of hearth stone bags



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



At this time, the directory will have one more salary File



Click it to see that the balance has been recorded

Thoughts:

It took two days to finish the shopping cart, but it was not the whole day. After all, I had to attend classes and study. This time, I am familiar with file operations and a review of some basic knowledge. After writing this article, I am very happy to be able to run the function. because of the many difficulties encountered in the middle, I solved one problem after another, but I still solved the problem by searching and asking online. After writing it, I feel very low. After all, I need to practice it too little. This program is suitable for beginners or beginners who have finished file operations. By the way, bugs still occur when you test the program, but the impact is not very big. Just don't modify the price multiple times. I also thought about how to solve this problem, that is, clearing the list, in this way, the data will not be read twice, but it is found that the data read for the second time is not the changed data. I am wondering if the list has been refreshed and cleared. Leave this question here first. All functions have been implemented, but the writing is really low. I will grasp the new posture later and change it later! There are also three levels of menus for the previous logon! If you are a beginner like me, you can take the python course of Alex. if you can see it and read it with patience, please give me some advice!

Okay, Life is short, use python!

The above Python Implementation of the shopping system (example) is all the content shared by the small editor. I hope to give you a reference and support for the customer's house.

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.