Python job -------- shopping cart optimization, python --------
Read Me: after the last simple Shopping Cart implementation, the following functions are now implemented after another upgrade and optimization:
1. There are customer operations and merchant operations to achieve, the customer can buy things, when the amount is insufficient reminder, finally press q to exit, print the shopping cart list
2. Merchants can add or modify product prices.
3.this time all commodity information is stored in the product.txt file, for example, Watch: 1000.
4. To run this program, the txt file containing the product information is as follows:
Mind Map:
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 Portal: 8 #1. store product information in the file 9 #2. purchased items, balance record 10 # merchant portal: 11 #1. you can add product 2. modify product price 12 # store product 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 (":") # use a colon to split the first and second data into 19 product_list.append (I Ndex, item) # added data 20 f. close () 21 22 def print_product_list (): 23 for index, item in enumerate (product_list): 24 print (index, item) 25 # user portal 26 # user shopping 27 def user_shopping (): 28 salary = input ("Enter your salary:") 29 print_product_list () 30 if salary. isdigit (): 31 salary = int (salary) 32 shopping_list = [] # store the user's shopping cart list 33 while True: 34 option = input ("which one you like (corresponding number):") 35 if option. isdigit (): 36 option = int (o Ption) 37 if option> = 0 and option <= len (product_list): 38 p_item = product_list [option] # selected item 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 ("Shopping Cart added successfully, your balance is still % s" % (salary) 46 else: 47 print ("your balance is insufficient, only % s RMB "% (salary) 48 else: 49 print (" incorrect input. Please enter it 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 portal 61 # merchant add commodity 62 def add_product (): 63 name_of_product = input ("enter the name of the product you want to add:") 64 price_of_product = input ("Enter the price of the product 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 commodity list 75 choice = input (" Enter your choice: ") 76 # name_of_change = input (" enter the name of the product you want to change ") 77 price_of_change = input (" Enter the price of the product 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 product 82 # c_num = int (p_item [1]) # convert to int type 83 for line in fil Einput. input ("product.txt", inplace = "% s" % (choice): # modify the input Selection line 84 line = line. replace ("% s" % (p_item [1]), "% s" % (price_of_change )). strip () 85 print (line) 86 exit ("modification successful! ") 87 else: 88 print (" invalid input ") 89 else: 90 if choice =" q ": 91 exit (" exit ") 92 93 94 95 96 97 98 99 100 101 102 103 104 if _ name _ = "_ main __": 106 print ("--------------------------" 107 "------------------------" 108 "\ n" 109 "welcome to the shopping menu" 110 "\ n" 111 "\ n" 112 "press B, please press c \ n "113" -------------------------- "114" ------------------------ ") 115 c_num = input (" Enter your choice :") # select 116 if c_num = "B": 117 p Rint ("--------------------------" 118 "------------------------" 119 "\ n" 120 "welcome to the merchant interface" 121 "\ n" 122 "\ n" 123 "Please press a to add a product, to change the price, please press c \ n "124" -------------------------- "125" ---------------------- ") 126 c_num2 = input (" Enter your choice: ") 127 if c_num2 =" ": 128 # implement the add product function 129 add_product () 130 if c_num2 = "c": 131 # implement the product price modification function 132 change_price () 133 else: 134 print ("incorrect 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 ("program exited due to incorrect input! ")
View Code
End: Are there any errors? Do you want to point them out -_-