Ideas:
1. First generate a user dictionary file, the element contains the user name, password, balance, shopping cart list, quantity
2. Import dictionary content to verify login and record user information
3. Homepage can choose to login, register and browse the product list
4. Register the module, according to the dictionary key value to determine whether the user exists, there is return, there is no creation, password word limit did not do, Getpass module in the window platform error, no write
5. Login module, verify the successful login according to the dictionary content, do not successfully ask whether to register, three times verification failed to quit the program
6. After successful login, determine whether there is a balance in the user dictionary, do not exist, ask the user to enter the amount, and save in the dictionary
7. Print the product list, according to the user's choice: quit the program, log off the user, check the shopping cart, recharge, purchase function
8. Some features of shopping are not in-depth, for example, after adding to the shopping cart to cancel the purchase, the number of shopping options, etc., to be optimized
9. When exiting the program, save the newly generated user information to the user dictionary file.
The first time you need to write the dictionary content to a file:
# -*- coding: utf-8 -*-import pickleuser_list = { ' Test1 ':{ ' username ': ' test1 ', ' userpasswd ': ' 123456 ', ' salary ': ', ' Shop_car ': ', ' shop_ Car_list ': ' }, ' Test ':{ ' username ': ' Test ', ' USERPASSWD ': ' ABCD ', ' salary ': ', ' Shop_car ': ', ' shop_car_list ': ' } }f = open (' e:/linux Study/python study/ Day2/user_list.txt ', ' WB ') Pickle.duMP (USER_LIST,F) F.close ()
# -*- coding: utf-8 -*-import picklef = open (' e:/linux Study/python study/ Day2/user_list.txt ', ' RB ') User_list = pickle.load (f) f.close () def wirte_logout (): f = open (' e:/linux Learning/python Learning/day2/user_list.txt ', ' WB ') Pickle.dump (user_list,f) f.close () returnproduct_list = [ (' iphone 6s ', 5299), (' Iphone 6s plus ', 5999), (' Sumsung s7 ', 3888), (' Sumsung s7 edge ', 5688), (' 360 f4 ', 799), (' Red rice note3 ', 899), (' Meizu mx6 ', 1999), (' Huawei mate ', 2799), ] Def index_page (): page = "[0] Login [1] registration [2] browse ' Print (' \ n ', ' test page '. Center (60, '-'),Page) returndef login (): print (' \ n ') global user count = 1 for i in range (5): if count <= 3: user = input (' User login: ') password = input (' secret Code: ') if user in user_ List.keys (): if user == user_list[user][' username '] and password == user_list[user][' USERPASSWD ']: msg = "Welcome to login!" " print (msg) break else: print (' username or password is incorrect, please login again! ') else: user_choice = input (' user does not exist, need to register ( y/n) ') if user_choice == ' y ': add_user () &Nbsp; break elif user_choice == ' n ': pass count += 1 elif Count > 3: exit (' Exceeded login count! Login failed ') returndef add_user (): global user exit_flag = False Print (' \ n ') while not exit_flag: username = input ('Please enter your user name: ') if username in user_list.keys (): print (' \ n username already exists, please enter another name \ n ') else: exit_flag = true userpasswd = input (' Please enter your password ') user_list[username] = {} user_list[username][' username '] = username user_list[username][' userpasswd '] = userpasswd user_list[username][' salary '] = ' user_list[username][' Shop_car '] = ' user_list[username][' shop_car_list '] = ' print (' \ n registered successfully, your username is:%s ' % username, ' \ n ') user = user_list[username][' username '] &Nbsp; returndef print_product_list (): print (' \ n Product list: \ n ') for item in enumerate (product_list): &NBSP;INDEX&NBSP;=&NBSP;ITEM[0]&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;P_NAME&NBSP;=&NBSP;ITEM[1][0] p_price = item[1][1] print (Index, ': ', P_name,p_price) returndef printending (): print (' The product you have previously purchased '. Center (+, '-') ') for item in user_list [User] [' Shop_car ']: a = user_list[user][' Shop_car '].index (item) print (' Commodity:%s price:%s quantity:%s ' % (user_ list[user][' Shop_car '][a][0],user_list[user][' Shop_car '][a][1],user_list[user][' shop_car_list '][a]) print (' End '. Center (+, '-')) print (' Your balance is:%s ' % user_list[user][' salary '], ' \ n ') returnexit_ Flag = falsewhile not exit_flag: index_page () index_user_choice = input (' Please enter the action you want to take: ') if index_user_choice == ' 0 ': login () exit_flag = True elif index_user_choice == ' 1 ': add_user () exit _flag = true elif index_user_choice == ' 2 ': print_Product_list () else: print (' input operation is invalid! ') print (' begin the shopping '. Center (+, '-'), ' \ n ') exit_flag2 = falsewhile not exit_flag2: if not user_list[user][' Salary ']: salary = input (' Your balance is 0, please recharge the amount: ') if salary.isdigit (): salary = int (Salary) user_list[user [' Salary '] = salary else: print (' The data type is incorrect, please enter a correct number! ') continue else : salary = user_list[user][' salary '] if not user_list[user][' Shop_car ']: shop_car = [] shop_car_list = [] else: shop_car = user_list[user][' Shop_car '] shop_car _list = user_list[user][' Shop_car_list '] print_product_list () print ("[Q=quit,c=check,l=logout,r=rechage]") user_choice = Input (' What do you want to buy? ') if user_choice.isdigit (): user_ Choice = int (User_choice) if user_choice < len (product_list): p_item = product_LIST[USER_CHOICE]&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF&NBSP;P_ITEM[1] <= salary: if p_item not in shop_car: shop_car.append (P_item) Shop_car_list.append (1) user_list[user][' Shop_car '] = shop_car user_list[user [' Shop_car_list '] = shop_car_list salary -= p_item[1] user_list[user][' Salary '] = salary print (' Added on the cart:%s, the price is:%s, your balance is:%s ' % (p_item[0],p_item[1],user_list[user][' salary '), ' \ n ') else: item_num = shop_car_list[shop_car.index (P_item)]+1 shop_car_list[shop_car.index (p_ Item)] = item_num user_list[user][' Shop_car_list '] = shop_car_list salary -= p_item[1] user_list[user][' Salary '] = salary print (' Added on the cart:%s, the price is:%s, your balance is: %s ' % (p_item[0],p_item[1],user_list[user][' salary '), ' \ n ') else: print (' Your balance is:%s, the balance is not enough to buy! ' % user_list[user][' salary ', ' \ n ') else: print (' The product you have chosen does not exist! ', ' \ n ') else: if user_choice == ' Q ' or user_choice == ' quit ': printending () print (' bye %s ' % user) wirte_logout () exit_flag2 = true elif user_ choice == ' C ' or user_choice == ' check ': printending () elif user_choice == ' l ' or user_choice == ' logout ': &nBsp; printending () login () elif user_choice == ' R ' or user_ choice == ' rechage ': salary_ Change = input (' Please enter recharge amount: ') if salary_change.isdigit (): salary_change = int (Salary_change) salary += salary_change user_list[user][' Salary '] = salary else: &nbsP; print (' The data type is incorrect, please enter a correct number! ') continue else: print (' Please enter legal characters! ', ' \ n ')
Watch the video to learn self-practice, there is a shortage of places please forgive me.
This article is from the "ld0381 Learning Journey" blog, please be sure to keep this source http://ld0381.blog.51cto.com/3318114/1835396
Python Practice--Shopping Cart Program