Python 7-job [shopping cart], python 7 days
Job description:
Shopping Cart program:
After the program is started, enter the user name and password. If this is the first logon, ask the user to enter the salary, and then print the product list
Allows users to purchase products based on product numbers
After you select a product, check whether the balance is sufficient. If the balance is sufficient, you will be charged directly. If the balance is insufficient, you will be reminded.
You can exit at any time. When you exit, the purchased items and balances are printed.
Key messages, such as balances and items that have been added to the shopping cart, must be highlighted during user use.
After the next login, the user enters the user name and password and returns directly to the last status, that is, the balance of the last consumption or the other, and can continue to purchase after logging on again
Allow query of previous consumption records
Let's start with the process!
The specific code is still being optimized. First, let's look at the code corresponding to the flowchart!
1 #! Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Author calmyan 4 import time, sys, OS 5 with open('user_file.txt ', 'R ', encoding = 'utf-8') as user_file: 6 user_list = eval (user_file.readline () # convert the character string 7 lock_name1_open('lock_name.txt ', 'r + ', encoding = 'utf-8') # open the lock user list file 8 lock_list = eval (lock_name.readline () # convert and lock the string 9 10 def userchar (_ user_name) in the user file ): # username detection function 11 while True: 12 if _ user_name in user_list And _ user_name not in lock_list: # judge whether the user exists and is normal 13 return True # returns True 14 break 15 elif _ user_name in user_list and _ user_name in lock_list: # determine if the user is locked 16 print ('your user has been locked, please contact the merchant to unlock! ') 17 break 18 else: 19 print ('the user you entered does not exist, please register first') 20 break 21 22 def confirm (): # exit confirmation function 23 qu = input ("whether to log out! Press \ 033 [31; 1mY/y \ 033 [0 m to exit? Press another key to log on again: ") 24 if qu = 'y' or qu = 'y': 25 print (" You have exited! ") 26 exit () # exit program 27 else: 28 pass 29 30 def user_pass (): 31 bool = True # define exit loop condition variable 32 while bool: 33 _ user_name = (input ('enter User name: ') 34 if userchar (_ user_name.strip (): # Run the function to judge the user name, remove space on both sides 35 count = 3 # define the number of locks 36 while count> 0: # if the number of password errors exceeds the number of exit password Input 37 # password = getpass. getpass ('enter password: ') 38 password = input ('enter password:') 39 if password = user_list [_ user_name]: # password verification completion loop 40 print ("password verification passed") 41 bool = False 42 print (" Welcome to \ 033 [32; 1 m % s \ 033 [0 m you have logged on successfully! "% _ User_name) 43 return _ user_name # return username 44 else: 45 count-= 1 # count decrease 46 print (" Password error please try again, remaining: \ 033 [31; 1 m % s \ 033 [0 m times! "% Count) 47 pass 48 else: 49 print (" \ 033 [31; 1 Mbit/s exceeds the number of tests, and the account is locked! Contact the seller! \ 033 [0 m ") 50 lock_list.append (_ user_name) # Add the user name to the lock list 51 lock_name.seek (0) # Move the pointer to the beginning, avoid generating multiple tuples 52 lock_name.write (str (lock_list) # Write the lock User File 53 lock_name.tell () # obtain the current pointer to avoid generating multiple tuples 54 lock_name.close () # Close the Locked User File 55 confirm () # exit the confirmation function 56 # continue # Return to the input user name location 57 else: #58 confirm () if a user name error occurs () 59 pass 60 61 def infor (): # open the user information function 62 with open ('user _ infor ', 'r +', encoding = 'utf-8') as user_infor: # Open the user shopping information file 63 infor_li St = eval (user_infor.readline () # convert the file string 64 return infor_list 65 66 # program start: 67 if OS. path. exists ('History '): # Check whether the file exists 68 pass 69 else: 70 with open ('History', 'w', encoding = 'utf-8') as user_history: # create a user account shopping Record File 71 user_history.write ('{}') # Write null value 72 pass 73 74 if OS. path. exists ('user _ infor '): # Check whether the file exists 75 pass 76 else: 77 with open ('user _ infor', 'w ', encoding = 'utf-8') as user_infor: # create a user account information record file 78 use R_infor.write ('{}') # Write null value 79 pass 80 81 print ("Welcome to \ 033 [31; 1mpython \ 033 [0 m shopping mall! ") 82 time_format = '% y-% m-% d % x' # defines the time format 83 times = time. strftime (time_format) # defines the time 84 _ user_name = user_pass () # assigns the verified user name to the variable 85 86 if _ user_name in infor (): # Skip 87 pass 88 else: 89 while True: 90 name_char = input ("First Login, please enter the salary (shopping fund):") if the user information is recorded ):") # user input salary (shopping fund) 91 if name_char.isdigit (): # Check whether the input is an integer 92 name_char = int (name_char) # convert it to int 93 with open ('History ', 'r + ', encoding = 'utf-8') as user_history: # Open the user account shopping record file 94 History_list = eval (user_history.readline () # Read the text as a dictionary 95 history_list [_ user_name] ={}# create a user shopping record 96 user_history.seek (0) # Move to the start 97 user_history.write (str (history_list) # Write the file 98 with open ('user _ infor ', 'r +', encoding = 'utf-8') as user_infor: 99 infor_list = eval (user_infor.readline () # convert the string 100 infor_list [_ user_name] = {"amount": name_char} 101 user_infor.seek (0) in the First Login User File) 102 user_infor.write (str (infor_list) # write user information 103 B Reak104 else: 105 print ('the amount cannot be 0 or negative. Please enter the correct amount! ') 106 continue107 # directly display the user information when you log on again. 108 infor_list = infor () # obtain the user information dictionary. 109 name_char = int (infor_list [_ user_name] ['ant']) # obtain the balance of 110 with open ('History ', 'R', encoding = "UTF-8") as user_history: # Read the User File 111 history_list = eval (user_history.readline ()) # user shopping information 112 shoplist = [] # shopping cart list 113 shop_count = 0 # shopping amount statistics 114 ### go to the product list ### 115 goodsname = [] # define product list 116 print ('user Name: \ 033 [32; 1 m % s \ 033 [0 m: '% _ user_name) # output user information 117 print ('your balance: \ 033 [31; 1 m % S \ 033 [0 m: '% name_char) 118 history = history_list [_ user_name] # user shopping record information 119 print ('your shopping record is as follows:') 120 for k, v in history. items (): # convert to list to output 121 print (k, v) # output 122 print ('Load item list :') 123 # Load dynamic 124 for I in range (40): 125 sys. stdout. write ('#') 126 sys. stdout. flush () 127 time. sleep (0.05) 128 print ('\ n') 129 130 131 with open('goodslist.txt', 'R', encoding = 'utf-8') as f: # Open the product list file 132 for line in f: 133 a = line. find (',') # locate the split 134 goodsname. append (line [0: a], int (line [a + 1:]) # The price is converted to an integer of 135 136 while True: 137 for index, p_item in enumerate (goodsname): # retrieve the subscript Output commodity list 138 print (index, p_item) # output product list 139 user_index = input ('enter the product number to add the product you purchased, and press \ 033 [31; 1mq/Q \ 033 [exit 0m >>>: ') 140 if user_index.isdigit (): # judge whether the integer is 141 user_index = int (user_index) # convert it to the number 142 if user_index <len (goodsname) and user_index> = 0: # determine the number of the selected item in the list 143 p_item = go Odsname [user_index] # Read the goods where the subscript is located (element) 144 if p_item [1] <= name_char: # whether the balance is sufficient 145 shoplist. append (p_item) # Add to shopping cart 146 name_char-= p_item [1] # deduct the amount of 147 shop_count + = p_item [1] # accumulate the purchase amount of 148 print ("add \ 033 [32; 1 m % s \ 033 [0 m to the shopping cart, price: \ 033 [32; 1 m % s \ 033 [0 m yuan "% p_item) 149 print ("your purchase amount remaining \ 033 [31; 1 m % s \ 033 [0 m yuan" % name_char) 150 else: 151 print ("your purchase amount \ 033 [41; 1 m % s \ 033 [0 m yuan, unable to buy this item! "% Name_char) 152 else: 153 print ('input error, this number \ 033 [41; 1 m % s \ 033 [0 m product does not exist! '% User_index) 154 elif user_index = 'q' or user_index = 'q': 155 print ('your shopping list:') 156 for up in shoplist: # print the shopping list 157 print (up) 158 print ("your total shopping amount is \ 033 [32; 1 m % s \ 033 [0 m RMB! "% Shop_count) 159 print (" your purchase amount: \ 033 [31; 1 m % s \ 033 [0 m RMB! "% Name_char) 160 while True: 161 confirm = input (" are you sure you want to quit shopping? \ 033 [31; 1 m Y/y \ 033 [0 m is \ 033 [31; 1mN/n \ 033 [0 m no :") 162 if confirm = 'y' or confirm = 'y': 163 infor_list [_ user_name] ['ant'] = name_char # modify the user amount (after shopping) 164 shoplist. append ('total: ', shop_count) # Add the total amount to the list. 165 history_list [_ user_name] [times + 'shopping list:'] = shoplist # add time: listing 166 with open ('user _ infor ', 'w', encoding = "UTF-8") as user_infor: # Write File user information 167 user_infor.seek (0) # Move to the top 168 user_infor.write (str (user_list) # Write 169 with open (' History ', 'r +', encoding = 'utf-8') as user_history: # Write the shopping information to the file 170 user_history.seek (0) # Move to the top 171 user_history.write (str (history_list) # Write 172 exit (print ("Thank you! Welcome to visit again! ") 173 elif confirm = 'n' or confirm = 'N': 174 break175 else: 176 print (" input error! Enter as prompted! ") 177 continue178 else: 179 print ('\ 033 [41; 1 m input item number error, please enter it again! \ 033 [0m ')
A little bit. I spent a day coding !! Still want better !!
Perfectionist: no harm !!