Python implements simple shopping malls and python shopping malls

Source: Internet
Author: User

Python implements simple shopping malls and python shopping malls

This article shares with you the shopping mall mini-app for your reference. The specific content is as follows:

Software Version:Python3.x
Function:Simple Shopping Mall
1. How many items can be selected?
2. Multiple users are allowed to log on. After the next login, continue to purchase based on the previous balance
3. allow users to view previous purchase records (display the purchase time)
4. Hierarchical display of the product list

Operation:
1. Default User, pan, li, password 123
2. After logging on, you must enter the correct user name and password.
3. Select the Recharge Amount as prompted
4. Select the purchased item, press q to exit, and press c to view the easy-to-purchase record, and press s to view the currently purchased item

Note:File_lock.txt,username_file.txt needs to be created in advance

The Code is as follows:

#! /Usr/bin/env python # coding: UTF-8 # Auto: Panwenbin # function: import shelveimport timeshop_list = {# define the product list 'ters': {' ': '123', 'camera ': '123', 'cell phone': '123', 'printer ': '123456'}, 'clothing Department a': {'jeans ': '000000', 'jacket ': '000000', 'wang laoji': '6', 'Instant noodle ': '4'}, 'auto': {'Tesla ': '20140901', 'bmw X5 ': '20160301', 'passay': '20160301', 'chery': '20160301'}, 'cosmetic ': {'oreal ': '20140901', 'oracle man': '20160301', 'hangzhou': '20160301', 'baique Impala ': '20160301 '},} count = 0 # define a counter jiage = [] shop_list1 = {} shop_name = list (shop_list.k Eys () Comment ', 'r +') count = 0def show_list (): # print the serial number and item classification list for I, j in enumerate (shop_list ): print ('% d: % s' % (I, j) def show_menu (user_choise): # print the serial number and the second-level list of the product user_choise = int (user_choise) a = shop_name [user_choise] for index, key in enumerate (shop_list [a]): # print the serial number and the second-level list print ('% d: % s % s' % (index, key, shop_list [a] [key]) def show _ Menu2 (user_choise, user_choise2): # Call the user's first selection and second selection to go to the corresponding shopping list user_choise = int (user_choise) if user_choise2.isdigit () and int (user_choise2) <len (shop_list [shop_name [int (user_choise)]): # judge whether the user input is a number and smaller than the product serial number user_choise2 = int (user_choise2) # if user_choise2 = 0: jiage. clear () for v, k in enumerate (shop_list [shop_name [user_choise]): # obtain the list of products that the user enters the second layer. append (shop_list [shop_name [user_choise] [k ]) # Jiage2 = (int (jiage [user_choise2]) global jiage2def quit_time (): # exit for I in [3, 2, 1] After the query is complete: print ('\ 033 [32; 1 m query is complete, returning to the main menu ..... \ 033 [1m', I) time. sleep (1) def start (): while True: if user_choise.isdigit () and int (user_choise) <len (shop_list): # judge whether the user inputs a number, and smaller than the product serial number show_menu (user_choise) # Call the show_menu function, break else: print ('\ 033 [31; 1 m invalid option, Please re-enter \ 033 [1 M ') # re-enter break ################################ ######################################## ######################################## ######## While count <3: # print repeatedly as long as no more than 3 retries ('\ 033 [31; 1 m default user name and password: pan, 123 li, 123 \ 033 [1 M ') username = input ('\ 033 [34; 1 m, enter your username \ 033 [1 M') for I in lock_file.readlines (): # determine whether the user name is in the lock file I = I. split () if username in I [0]: print ('\ 033 [31; 1 m sorry % s locked \ 033 [1 M' % username) exit () match = False for j in user_pass.readlines (): user, pas Sword = j. strip ('\ n '). split () # Remove the redundant \ n of each line and divide this line into two columns by space. The values are respectively user and passwd variables if username = user: # determine whether the input user has passwd = input ('\ 033 [30; 1 m; enter password:') if password = passwd: match = True break elif password! = Passwd: # If the user name is correct, determine whether the entered password is correct for I in range (2): passwd = input ('\ 033 [31; 1 m Password error, please re-enter the password \ 033 [1m ') if password = passwd: match = True break else: print (' \ 033 [31; 1 m password does not match the user name, after three attempts, the user is locked \ 033 [1m ') lock_file.write (' % s \ n' % username) lock_file.close () user_pass.close () exit () if username! = User: print ('\ 033 [31; 1 m you entered the user name does not exist, the program has exited \ 033 [1 M') exit () elif match = True: break ####################################### ######################################## ######################################## # f = shelve. open ('user. db', 'C + ') try: if f [user]> 0: pay = f [user] chong_zhi = input (' \ 033 [33; 1 m your current balance is % d. Do you want to recharge? Enter the amount you want to recharge. Any key is required to go to the next step. to exit the program, press q: \ 033 [1m '% f [user]) if chong_zhi.isdigit () and int (chong_zhi)> 0: pay = int (chong_zhi) + int (pay) print ('\ 033 [35; the value after 1 m recharge is \ 033 [1 M', pay) failed t KeyError: chong_zhi = input ('\ 033 [36; 1 m your current balance is 0. Do you want to recharge? Enter the amount you want to recharge. to exit the program, press q: \ 033 [1m ') while True: if chong_zhi.isdigit () and int (chong_zhi)> 0: pay = int (chong_zhi) print ('\ 033 [33; the value after 1 m recharge is \ 033 [1 M', pay) break elif chong_zhi = 'q ': print ('\ 033 [36; 1 M program is exiting \ 033 [1 M') exit () else: pass # pay = input ('your current balance is 0. Do you want to recharge? Enter the amount you want to recharge. to exit the program, press q :') ######################################## ######################################## ######################################## while True: show_list () # Call the show_list function to print the commodity classification list user_choise = input ('\ 033 [32; 1 m select the type of the purchased commodity: \ 033 [1 M ') # Get the category of the selected item start () # Call the start function user_choise2 = input ('\ 033 [36; 1 m to select the type of the item to be purchased. Press q to exit, press c to view easy purchase records, and press s to view the currently purchased items. Any key will return to the top menu: \ 033 [0m ') # obtain the selected item # user_num = input ('\ 033 [35; 1 m select the number of items to be purchased, default value: 1: \ 033 [0m ') # obtain the number of items selected by the user. if user_choise2.isdigit () and int (user_choise2) <len (shop_list [shop_name [int (user_choise)]): # determine whether the user inputs a number, and smaller than the product serial number user_num = input ('\ 033 [35; 1 m, select the number of items to be purchased, the default value is 1: \ 033 [1 M ') # obtain the number of items selected by the user. show_menu2 (user_choise, user_choise2) # Call the show_menu2 function to obtain the type and quantity of items selected by the user. if user_num.strip () = '': # If the user input is empty, the default value is 1 user_num = int (1) elif user_num.isdigit and int (user_num)> 1: # If the user input is a number that is equal to or greater than 1, obtain user_num = int (user_num) else: user_num = int (1) in user input # In other cases, the default value is pay = int (pay) if pay> jiage2 * user_num: # determine whether the price * quantity of the selected item can be paid pay = pay-jiage2 * user_num choise = list (shop_list [shop_name [int (user_choise)]. keys () # obtain the list of commodities that the user enters Layer 2: count + = 1 # counter value plus goumai_jiluw.shelve.open('goumai_jilu.txt ', 'a + ') # Call shelve to open a data file goumai_jilu [str (count)] = choise [int (user_choise2)], jiage2, user_num, time. ctime () # Insert the user-selected item to the data file. The number and purchase time are goumai_jilu.close () # Close the data file a = choise [int (user_choise2)] # obtain the selected item, choise selects the second-level product list for the user if a in shop_list1: # if the selected product has purchased shop_list1 [a] [0] = int (shop_list1 [a] [0]) + user_num # Add shop_list1 [a] [1] = int (shop_list1 [a] [1]) + int (shop_list1 [a] [1] * user_num) # Add the total amount to else: shop_list1 [a] = list ([1, jiage2]) print ('\ 033 [32; 1 m your balance is: \ 033 [1 M ', pay) else: print ('\ 033 [32; 1 m your balance is insufficient. Please reselect: \ 033 [0m') elif user_choise2 =' B ': # Return to the product category list continue elif user_choise2 = 'q': # exit program f = shelve. open ('user. db', 'a + ') f [user] = pay f. close () exit () elif user_choise2 = 'C': goumai_jilu=shelve.open('goumai_jilu.txt ', 'a +') print ('\ 033 [32; 1 m your current purchase record is: \ 033 [1m ') for I in goumai_jilu.items (): # obtain the data print (' {}{} 'in the purchase record ('{}{}'. format (I [0], goumai_jilu [I [0]) # print the purchase record print ('\ 033 [32; 1 m ###################################### ########## \ 033 [0m ') quit_time () elif user_choise2 = 's': print ('\ 033 [32; 1mp_name num total_price \ 033 [1m') print ('\ 033 [32; 1 m %-10 s %-10 s %-10s \ 033 [1m '% (a, shop_list1 [a] [0], shop_list1 [a] [1]) # print the number and total number of items currently purchased. print ('\ 033 [32; 1 m ###################################### ########## \ 033 [0m ') quit_time () else: print ('\ 033 [41; 33m invalid option, please reselect: \ 033 [0m') continue

The above is all the content of this article. I hope it will help you learn Python programming.

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.