Python program Exercise 3 -- simulate a shopping cart, python3 --
1. Features
This program simulates the user's purchase of goods after logging on to the mall. Allows users to log on, purchase commodities, query historical consumption records, and update balances and consumption information. Enter the initial account funds for the first login, and then log on to the system to obtain the balance after the previous consumption from the file. After each purchase, the corresponding amount will be deducted and the balance information will be updated, when you exit, the balance and consumption records are updated to the file for subsequent queries.
2. Implementation Method
This program is written in the python language to break down various tasks and define corresponding functions for processing, so that the program structure is clear and clear. I mainly wrote six functions:
(1) login (name, password)
The user logs on to the function to verify the user name and password. If the login succeeds, the number of logins is returned.
(2) get_balance (name)
Obtain user balance data.
(3) update_balance (name, balance)
Updates the user balance data. When the user exits by pressing the q key, the data is updated to the file.
(4) inquire_cost_record (name)
Query historical user consumption records.
(5) update_cost_record (name, shopping_list)
The user consumption record is updated. When the user exits by pressing the q key, the consumption record is updated to the file.
(6) shopping_chart ()
The main function completes human-computer interaction, function calling, and orderly implementation of various functions.
(1) follow the prompts and press the number key to select the appropriate options for the operation.
(2) press the q key at any time to exit the logon. Before exiting, the user consumption and balance information are updated.
- Usage file: (1)userlist.txt
Stores user account information files, including user name, password, logon times, and balance. The number of user logins will be updated each time the user logs in successfully, and the balance information will be updated each time he or she presses the q key to exit.
(2) *** _cost_record.txt
A file that stores a user's ** consumption record. This file is created after the user buys the product for the first time. users who have not purchased the product will not generate this file. Every time you press the q key to exit, the latest consumption record is updated to the file.
3. Flowchart
4. Code
1 # Author: Byron Li 2 #-*-coding: UTF-8-*-3 4 ''' ------------------------------------------------ instructions for using the file UI 5 instructions for using the file 6 userlist.txt to store user account information files, this includes the user name, password, login times, and balance of 7 *** _cost_record.txt, which stores the file of a user's *** consumption record. After the user buys the product for the first time, users who have not purchased the product will not generate this file 8 comment ---------------------------------------------------------------------------------------------------------------------'' '9 import OS 10 import datetime 11 12 def login (name, password): # user login, user name and password verification. If login is successful, return login count 13 with open('userlist.txt ', 'r + ', encoding = 'utf-8') as f: 14 line = f. readline () 15 while (line): 16 pos = f. tell () 17 line = f. readline () 18 if [name, password] = line. split () [0: 2]: 19 times = int (line. split () [2]) 20 line = line. replace (str (times ). center (5, ''), str (times + 1 ). center (5, '') 21 f. seek (pos) 22 f. write (Line) 23 return times + 1 24 return None 25 26 def get_balance (name): # Get user balance data 27 with open('userlist.txt ', 'R', encoding = 'utf-8 ') as f: 28 line = f. readline () 29 for line in f: 30 if name = line. split () [0]: 31 return line. split () [3] 32 print ("User % s does not exist and cannot obtain the balance information! "% Name) 33 return False 34 35 def update_balance (name, balance): # update user balance data 36 with open('userlist.txt ', 'r +', encoding = 'utf-8 ') as f: 37 line = f. readline () 38 while (line): 39 pos1 = f. tell () 40 line = f. readline () 41 if name = line. split () [0]: 42 pos1 = pos1 + line. find (line. split () [2]. center (5, '') + 5 43 pos2 = f. tell () 44 f. seek (pos1) 45 f. write (str (balance ). must ust (pos2-pos1-2, '') 46 return True 47 print ("The USER % s does not exist and cannot update its balance information! "% Name) 48 return False 49 50 def inquire_cost_record (name): # query historical user consumption records 51 if needed]): 52 with open('''.join({name,'_cost_record.txt ']), 'R ', encoding = 'utf-8') as f: 53 print ("historical consumption records ". center (40, '=') 54 print (f. read () 55 print ("". center (46, '=') 56 return True 57 else: 58 print ("You have no history consumption records yet! ") 59 return False 60 61 def update_cost_record (name, shopping_list): # update a user's consumption record 62 if len (shopping_list)> 0: 63 if not OS. path. isfile (''. join ([name, '_cost_record.txt ']): # When the first line is created, the first line is marked with "commodity price" 64 with open (''. join ([name, '_cost_record.txt ']), 'A', encoding = 'utf-8') as f: 65 f. write ("%-5 s % + 20s \ n" % ('item ', 'price') 66 f. write (''. join ([datetime. datetime. now (). strftime ('% C'), 'consumption record']). center (40, '-') # Write consumption Facilitate subsequent query of Inter-Domain Information 67 f. write ('\ n') 68 for product in shopping_list: 69 f. write ("%-5 s % + 20s \ n" % (product [0], str (product [1]) 70 else: 71 with open (''. join ([name, '_cost_record.txt ']), 'A', encoding = 'utf-8') as f: 72 f. write (''. join ([datetime. datetime. now (). strftime ('% C'), 'consumption record']). center (40, '-') 73 f. write ('\ n') 74 for product in shopping_list: 75 f. write ("%-5 s % + 20s \ n" % (product [0], str (product [1]) 76 ret Urn True 77 else: 78 print ("You have not purchased this time, do not update the consumption record! ") 79 return False 80 81 def shopping_chart (): # main function, user interaction, function call, result output 82 product_list = [83 ('iphone ', 5000 ), 84 ('bicycle ', 600), 85 ('lenovo computer', 7800), 86 ('shirt ', 350), 87 ('washing machine', 1000 ), 88 ('Mineral water ', 3), 89 ('watches', 12000) 90] # store commodity list 91 shopping_list = [] # The user buys the commodity list 92 while (True ): 93 username = input ("Enter the User name:") 94 password = input ("enter the password:") 95 login_times = login (username, password) # Check whether the entered username and password are correct. if the entered username and password are correct, 96 if login _ is returned _ Times: 97 print ('Welcome % s % d login! '. Center (50, '*') % (username, login_times) 98 if login_times = 1: 99 balance = input ("Enter the salary :") # For the first login, enter account funds 100 while (True): 101 if balance. isdigit (): 102 balance = int (balance) 103 break104 else: 105 balance = input ("Incorrect salary input, please enter again:") 106 else: 107 balance = int (get_balance (username) # Not the first login to obtain the account balance from the file 108 while (True): 109 print ("Please select whether you want to query the consumption record or purchase the product: ") 110 print (" [0] querying consumption records ") 111 print (" [1] purchasing products ") 112 choice = input (" >>> ") 113. if choice. isdigit (): 114 if int (choice) = 0: # query historical consumption records 115 inquire_cost_record (username) 116 elif int (choice) = 1: # Purchase Product 117 while (True): 118 for index, item in enumerate (product_list): 119 print (index, item) 120 choice = input ("Enter the product number to purchase the product: ") 121 if choice. isdigit (): 122 if int (choice)> = 0 and int (choice) <len (product_list): 123 if int (product_list [int (choice)] [1]) <balance: # Check whether the balance is sufficient. If the balance is sufficient, the product is successfully purchased at 124 shopping_lis. T. append (product_list [int (choice)]) 125 balance = balance-int (product_list [int (choice)] [1]) 126 print ("\ 033 [31; 1 m % s \ 033 [0 m has been added to the shopping cart. Your current balance is \ 033 [31; 1 m % s RMB \ 033 [0 m "% (product_list [int (choice)] [0], balance) 127 else: 128 print (" \ 033 [41; 1 m your balance is only % s RMB, cannot purchase % s! \ 033 [0 m "% (balance, product_list [int (choice)] [0]) 129 else: 130 print (" incorrect input number. Please enter it again! ") 131 elif choice = 'q': # log out of the account and log out. Print the purchase list and balance information before exiting, and update it to file 132 if len (shopping_list)> 0: drawing print ("this purchase item list ". center (50, '-') 134 for product in shopping_list: 135 print ("%-5 s % + 20 s" % (product [0], str (product [1]) 136 print ("". center (50, '-') 137 print ("your balance: \ 033 [31; 1 m % s RMB \ 033 [0 m" % balance) 138 update_cost_record (username, shopping_list) 139 update_balance (username, balance) 140 print ("log out! ". Center (50, '*') 141 exit () 142 else: 143 print (" You have no purchase record this time. Welcome to purchase it next time! ") 144 print (" log out! ". Center (50, '*') 145 exit () 146 else: 147 print (" option input error. Please enter it again! ") 148 else: 149 print (" option input error. Please try again! ") 150 elif choice = 'q': # log out of account 151 print (" log out! ". Center (50, '*') 152 exit () 153 else: 154 print (" option input error. Please enter it again! ") 155 break156 else: 157 print ('user name or password is incorrect. Please enter it again! ') 158 159 shopping_chart () # main program running
View Code