Shopping Cart Program requirements: User information:
{' Wyf ': {' password ': ' A ', ' role ': ' 2 ', ' money ': ' 100000 ', ' carts ': [' iphone6 ', ' MI5 ']},
' Fei ': {' password ': ' A ', ' role ': ' 2 ', ' money ': ' 100000 ', ' carts ': [' iphone6 ', ' MI5 '}}
User role role:1 on behalf of the Administrator, 2, on behalf of ordinary users
The normal user's privileges are:
1. View all products;
2. Enter the product name to buy the goods, add to the shopping cart after purchase, the money will be subtracted. The purchased item is added to the cart.
3. Can view your balance, and shopping cart items
4. Exit
Admin User:
1. Add the item, enter the product name and balance
2. Recharge, enter the user name, give him the price
3. Exit
Thinking Analysis: Each small operation branches encapsulated into a small function, the last user can call. The knowledge points used in this application are: function, file modification, menu. The current program also has a shortage of places, the code is as follows
defUser_balance (name):#Check the user balance, and the items in the shopping cartf = open ("User.txt","r+") User_info=eval (F.read ())Print(User_info)Print("your balance is:"+ STR (user_info[name][" Money"])) Print("the goods in the car are:"+ STR (user_info[name]["Carts"]))defuse_user_balance (): Name=username user_balance (name)defAdd_product (Inp_product,inp_price):#Existing product changes prices, non-existent items and prices are added directly.FW = Open ('Product.txt','A +') Fw.seek (0) product_dict=eval (Fw.read ()) product_dict[inp_product]= {' Price': Inp_price}Print(product_dict) fw.seek (0) fw.truncate () fw.write (str (product_dict)) Fw.flush ()defuse_add_product (): Inp_product= Input ("Please enter the item you want to add:") Inp_price= Input ("Please enter the price of your product:") add_product (inp_product,inp_price)defall_product (): F= Open ("Product.txt","r+") Product_list=eval (F.read ())Print("The products of our store are:"+str (product_list))defBuy_product (Inp_product,name):#users Buy Productsf = open ("User.txt","r+") FW= Open ('Product.txt') User_info=eval (F.read ()) Product_list=eval (Fw.read ())ifInp_product not inchproduct_list:Print("Sorry, the item you entered does not exist, please enter the correct product") Else: User_money= Int (user_info[name][" Money"]) Product_price= Int (product_list[inp_product][" Price"]) ifUser_money >Product_price:Print("can buy") Balance= User_money-Product_price f.seek (0) user_info[name][' Money'] =Balance Carts_list= user_info[name]['Carts'] Carts_list.append (inp_product) f.truncate () f.write (str (user_info)) F.FL Ush ()Else: Print("Sorry, your account balance is not enough, please contact the administrator to recharge!")defuse_buy_product (): Name=username Inp_product= Input ("Please enter the name of the product you want to buy:") buy_product (inp_product, name)defUser_add_money (Name,inp_money):#Enter user name to recharge the userf = open ('User.txt','r+') F.seek (0) User_info=eval (F.read ())#print (user_info)New_money = Int (user_info[name][' Money']) +Int (inp_money) user_info[name][' Money'] =New_moneyPrint(user_info) f.seek (0) f.truncate () f.write (str (user_info)) F.flush ()defUse_user_add_money (): Name= Input ("users who need to recharge:") Inp_money= Input ("How many dollars this recharge:") User_add_money (name, Inp_money)defExit_sys (): Exit ('Exit Program') F= Open ("User.txt","r+") User_info=eval (f.read ()) Username= Input ("Please enter your user name:") Password= Input ("Please enter your password:")ifUsername not inchUser_info:Print("the user name is not correct, please try again")Else: ifPassword = = user_info[username]["Password"]: Print("Welcome"+ Username +"Log in to the shopping cart!") ifuser_info[username]["role"] =='1': Print("Hello, you are the Administrator") Choice= Input ('Please select your action: 1. Add Item 2. User Recharge 3. Exit') menu1= { "1": Use_add_product,"2": Use_user_add_money,"3": Exit_sys} Menu1[choice] ()Else: Print("Hello, you are a regular member") Choice= Input ('Please select your action: 1. View all products 2. Purchase goods 3. View balance and purchased items 4. Exit') menu2= { "1": All_product,"2": Use_buy_product,"3": Use_user_balance,"4": Exit_sys} Menu2[choice] ()Else: Print("The password is not correct! Please try again")
Python Simple Shopping Cart program parsing