Compile a shopping mall in day2 and a shopping mall in day2

Source: Internet
Author: User

Compile a shopping mall in day2 and a shopping mall in day2

Assignment: Shopping Mall

Product Display, price

Buy, add to shopping cart

Payment, insufficient money

The flowchart is as follows:

The Code contains four files:

User Files:

 

alex 666geng 888zhang 222lou 250zeng 333

 

Product file:

10001 Xiaomi 3 2699 5010002 BYD song 100001 9110003 gree Inverter Air Conditioner 20000 410004 tcl TV 6000 9810005 Lenovo 001 5600 99210006 running chicken 250 66310007 Volkswagen 58000 9710008 Mazda 68000 43

Shopping Cart file:

10002 BYD song 100001 1 geng10006 running chicken 250 1 geng10008 Mazda 68000 1 geng10003 gree Inverter Air Conditioner 20000 1 geng

Wallet files:

geng 115880zeng 126748

The Code is as follows:

Import sysdef show (): '''item display module ''' shop_lists = [] shoppings = [] print ("item No.: Item Name: item price: Item Inventory :", end = '') print ("\ n *********************************** **************************************** **********") with open ("shoppings", "r") as f: for line in f: (shopping_num, shopping_name, shopping_price, shopping_stock) = line. strip (). split () shop_lists.append (shopping_num) shoppings. append ([shopping_num, shop Ping_name, shopping_price, shopping_stock]) print (shopping_num + "\ t", shopping_name + "\ t", shopping_price + "\ t", shopping_stock + "\ t ") return (shop_lists, shoppings) def login (user): ''' logon module, user verification user Logon status ''' users ={} with open ("users_file ", "r") as user_f: for user_line in user_f: username, password = user_line.strip (). split () users [username] = password if user in users. keys (): while True: pwd = input ("Enter Your password: ") if pwd = users [user]: print (" Hello, welcome % s! "% User) active = False return active else: print (" sorry, the password you entered is incorrect. Please enter it again! ") Else: print (" sorry, the account you entered is incorrect. Check the account and try again! ") Return Truedef shopping_cart (stock, user, stock_list, shoppings): ''' shopping cart module, add the item ''' shop_lists = [] if stock in stock_list to the shopping cart: while True: num = input ("Enter the quantity of items you want to purchase:") if int (num) <= int (shoppings [stock_list.index (stock)] [3]): shop_lists.append ([stock, shoppings [stock_list.index (stock)] [1], shoppings [stock_list.index (stock)] [2], num, user]) shoppings [stock_list.index (stock)] [3] = str (int (shoppings [sto Ck_list.index (stock)] [3])-int (num) break elif int (num)> int (shoppings [stock_list.index (stock)] [3]): print ("sorry, the quantity you entered exceeds the inventory. Please enter it again! ") Else: print (" sorry, the product number you entered does not exist. Please check it and enter it! ") With open (" cart_file "," a + ") as append_f: for shop_list in shop_lists: user_line = "". join (shop_list) append_f.write (user_line + "\ n") return shoppingsdef balance (user): # Calculate the current shopping consumption costs = [] with open ("cart_file", "r ") as balance_f: for line in balance_f: num, name, price, stock, username = line. strip (). split () if username = user: costs. append (int (price) * int (stock) price = 0 for cost in costs: pric E + = cost return pricedef recharge (user): '''recharge module ''' money = input ("Enter the amount you want to recharge :") user_wallets = [] with open ("wallet_file", "r") as charge_f: for line in charge_f: username, bal = line. strip (). split () if username = user: bal = str (int (bal) + int (money) user_wallets.append ([username, bal]) else: user_wallets.append ([username, bal]) with open ("wallet_file", "w") as recharge_f: for user_wallet in user _ Wallets: recharge_f.write ("". join (user_wallet) + "\ n") def account (user, price ): # Here we use recursive methods to settle user = user price = price wallets = [] with open ("wallet_file", "r") as obj_f: for line in obj_f: username, purse = line. strip (). split () if username = user: if int (purse)> = int (price): purse = str (int (purse)-int (price )) print ("Payment successful! ") Wallets. append ([username, str (purse)]) else: print ("sorry, your balance is insufficient. Please recharge") recharge (user) account (username, purse) # recursively execute the function, knowing that the amount is greater than else: wallets. append ([username, str (purse)]) return walletsif _ name _ = "_ main __": active = True message = '''******************************** **************************************** * ****************** \ 033 [32; 1 m welcome to the pig shopping website, wish you a pleasant shopping! \ 033 [0 m *********************************** **************************************** * **************** ''' print (message) lists = show () while active: user = input ("Enter your Username:") active = login (user) global ShoppingStock global ShoppingLists ShoppingStock = lists [0] # define the global variable to get the product ID, which is in the list, the purpose is to obtain the index number ShoppingLists = lists [1] # define global variables and store the information for traversing the product. The purpose is to easily write a file # Add the product to the shopping cart while True: shopping_index = input ("Enter the number of the product you want to purchase (enter quit to exit shopping):") if shopping_index = "quit": # When the user inputs quit, exit break else: ShoppingLists = shard (shopping_index, user, ShoppingStock, ShoppingLists) with open ("shoppings", "w") as modify_f: for shopping_list in ShoppingLists: shopping_line = "". join (shopping_list) + "\ n" modify_f.write (shopping_line) price = balance (user) wallets = account (user, price) with open ("wallet_file", "w ") as wallet_f: for user_line in wallets: wallet_f.write ("". join (user_line) + "\ n ")
The running result is as follows:

**************************************** **************************************** ***********

Welcome to the pig shopping website and wish you a pleasant shopping!

**************************************** **************************************** ***********

Product No.: Product Name: product price: product inventory:
**************************************** **************************************** *****
10001 Xiaomi 3 2699 50
10002 BYD song 100001 92
10003 gree Inverter Air Conditioner 20000 5
10004 tcl TV 6000 98
10005 Lenovo 001 5600 992
10006 running chicken 250 664
10007 Volkswagen 58000 97
10008 Mazda 68000 44
Enter your Username: geng
Enter your password 888
Hello, geng!
Enter the number of the product you want to purchase (enter quit to exit shopping): 10002
Enter the number of items you want to purchase: 1
Enter the number of the product you want to purchase (enter quit to exit shopping): 10006
Enter the number of items you want to purchase: 1
Enter the number of the product you want to purchase (enter quit to exit shopping): 10008
Enter the number of items you want to purchase: 1
Enter the number of the product you want to purchase (enter quit to exit shopping): 10003
Enter the number of items you want to purchase: 1
Enter the number of the product you want to purchase (enter quit to exit the shopping): quit
Sorry, you have insufficient balance. Please recharge your account
Enter the amount you want to recharge: 100000
Payment successful!

The above code runs as follows:

(1) display product information;

(2) user login verification;

(3) The user enters the desired product and quantity, and enters quit to exit the shopping;

(4) Add the file to the shopping cart;

(5) Settlement: go to the shopping cart to calculate the shopping expense;

(6) Call the user's wallet file to check whether the balance is sufficient;

(7) If the balance is greater than or equal to the same amount of shopping, the payment is successful. If the balance is insufficient, the user will recharge the account;

(8) Call the recharge module to recharge the account;

(9) recursively determine that the balance after the user's recharge is greater than or equal to the current shopping expense, and the payment is successful;

(10) Terminate the program.

Knowledge:

(1) The method of list traversal. to modify a list, you must first read the list and then modify it based on the information entered by the user;

(2) open and close the file;

(3) apply recursion, that is, when the user's balance remains small, it is recursive until the user's balance is greater than the cost of the purchase;

(4) cycle start and end (break), program start and end (sys. exit), function start and end (return );

(5) solve the problem of the Association and index of the list and the sequence of reading files.

 

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.