购物车程序更新:
更新商家入口,实现以下功能:
1. 商家能够修改商品价格;
2. 商家能够下线商品;
3. 商家能够增加商品;
4. 商品信息存在文件中
1 # -*- coding:utf-8 -*- 2 # LC 3 product_list = [] 4 product_line = [] 5 count = 0 6 7 with open("product_list","r") as f: #打开商家货品清单文件 8 for item in f: 9 product_line = item.rstrip().split(",") #将每行赋值给成一个列表,以“,”区分10 product_list.append(product_line) #将每行的列表加入product_list列表中11 12 print("This is your production".center(50,"-")) #打印列表13 for i in product_list:14 print(count,i)15 count+=116 17 #提供增加商品,修改商品价格,删除商品功能18 while True:19 select = input("Please input you selection,'a' to add,'m' to modified the price,'d' to Delete,'q' to quit :")20 count = 021 if select == 'a':22 add_product = input("Please input new product name:")23 add_price = input("Please input new product price:")24 new_product = [add_product,add_price] #将新添加的商品赋值成列表25 product_list.append(new_product) #向产品清单列表中插入新添加的商品26 with open("product_list","a") as f_add: #将添加的商品写入文件中,以追加的方式27 f_add.writelines(product_list[-1][0]+","+product_list[-1][1]+"\n")28 print("\033[1;31mThis is your new production list\033[0m".center(50,"-"))29 for i in product_list: #打印新的产品清单30 print(count,i)31 count+=132 elif select == 'm':33 modify_index = input("Please input index of the item which you want to modify:") #输入要修改价格的产品序号34 modify_index = int(modify_index)35 length = product_list.index(product_list[-1]) #判断输入的修改价格产品序号是否再产品清单范围36 if 0 <= modify_index <= length:37 new_price = input("Please input the new price:") #输入新的价格38 product_list[modify_index][1]=new_price #将新的价格修改至产品清单列表中39 with open("product_list","w") as f_modify: #想修改的产品清单列表写入文件中40 for i in product_list:41 f_modify.writelines(i[0]+","+i[1]+"\n")42 print("\033[1;31mThis is your new production list\033[0m".center(50,"-"))43 for i in product_list: #打印新的产品清44 print(count,i)45 count+=146 else:47 print("Your select is invalid, Please try again!") #如果输入的序号不在范围内,则报错重新来48 elif select == 'd': #删除商品49 delete_index = input("Please input index of the item which you want to delete:")50 delete_index = int(delete_index)51 length = product_list.index(product_list[-1]) #判断输入的删除序号是否再产品清单范围52 if 0 <= delete_index <= length:53 product_list.pop(delete_index) #从产品清单中删除制定的产品54 with open("product_list","w") as f_del:55 for i in product_list:56 f_del.writelines(i[0]+","+i[1]+"\n") #将新的产品清单写入文件中57 print("\033[1;31mThis is your new production list\033[0m".center(50,"-"))58 for i in product_list:59 print(count,i)60 count+=161 else:62 print("Your select is invalid, Please try again!")63 64 elif select == "q":65 print("You have been quit!")66 break67 else:68 print("invalid input,please input again!")
pic,2rice,3water,32phone,5000pia,300
更新用户购买程序,实现以下功能:
1. 用户第一次购买输入薪资;
2. 用户购买商品信息写入文件;
3. 用户再次购买先读取之前的购买清单文件,查看余额;
1 # -*- coding:utf-8 -*- 2 # LC 3 import os 4 product_list = [] #定义商品清单列表 5 product_line = [] #定义商品清单中每行的列表 6 user_buy_list = [] #定义用户已经购买商品列表 7 user_buy_product = [] #定义用户购买单个商品的列表 8 with open("product_list","r") as f_production: #读取商品清单列表 9 for item_production in f_production:10 product_line = item_production.rstrip().split(",")11 product_list.append(product_line)12 13 if os.path.getsize("shopping_list") == 0: #判断是否为第一次消费,如果消费清单文件为空,则第一次消费14 salary = input("Please input your salary:")15 balance = int(salary) #将余额定义为薪资16 else:17 length = len(open("shopping_list","rU").readlines()) #如果不是第一次消费,计算消费清单文件行数,提取消费列表18 count = 019 with open("shopping_list", "r") as f_shopping_list:20 for shopping_item in f_shopping_list:21 count +=122 if 2 <= count <= length-2: #去除第一行和最后两行,将消费清单中已购买的商品提取出来23 shopping_item = shopping_item.strip().split() #将购买的商品转换为列表,包含商品,单价,数量24 user_buy_list.append(shopping_item) #将已经购买的商品记录至用户消费清单列表中25 elif count == length:26 shopping_item = shopping_item.strip().split()27 balance = int(shopping_item[-1]) #如果非第一次消费,则余额为消费清单文件中的余额,提取出来28 while True:29 print("This is the production list".center(50, "-"))30 count = 031 for i in product_list:32 print(count, i)33 count += 134 user_select = input("Your balance is %d, 's' to start to shopping,'q' to quit : "%(balance))35 if user_select == 's':36 user_buy_product = []37 user_select_index = input("Please select the index of the one you want to buy:") #输入要购买的商品38 user_select_count = input("Please input the number how many you want to buy:") #输入要购买的商品数量39 user_select_index = int(user_select_index)40 user_select_count = int(user_select_count)41 unit_price = int(product_list[user_select_index][1]) #要购买的商品单价42 product_price = unit_price * user_select_count #购买的商品金额,即单价乘以总价43 if product_price <= balance:44 user_buy_product.append(product_list[user_select_index][0]) #将购买的商品的名称插入新的单个列表中45 user_buy_product.append(product_list[user_select_index][1]) #将购买的商品的单价插入新的单个列表中46 user_buy_product.append(str(user_select_count)) #将购买的商品的数量插入新的单个列表中47 user_buy_list.append(user_buy_product) #将新购买的商品,包含名称,单价,数量插入用户购买清单中48 balance = balance - product_price #将金额出去已经购买的商品金额,以免余额不足49 else: #购买的商品和数量,余额不足50 print("\033[1;31mYour choice is out of your money!\033[0m")51 continue52 elif user_select == 'q':53 break54 else:55 print("invalid input ,try again")56 continue57 58 #统计用户购买商品的总金额和消费余额59 total_price = 060 for i in user_buy_list:61 user_buy_product_price = int(i[1]) * int(i[2]) #用户购买的单个商品的总额,即单件商品的单价乘以数量62 total_price = total_price + user_buy_product_price #统计消费总金额63 total_consumption = ["Total_Consumption:"]64 balance_list = ["Your balance:"] #消费清单上记录余额信息65 balance_list.append(balance)66 total_consumption.append(total_price)67 user_buy_list.append(total_consumption) #将总消费金额信息插入用户消费清单中68 user_buy_list.append(balance_list) #将余额信息插入用户消费清单中69 70 #将用户购买的商品写入消费清单文件中,并记录总价和余额71 with open("shopping_list","w") as f_buy:72 f_buy.writelines("product".center(15,"-")+"price".center(15,"-")+"count".center(15,"-")+"\n")73 for i in user_buy_list:74 if len(i) == 3: #将商品写入75 f_buy.writelines(str(i[0]).center(15," ")+str(i[1]).center(15," ")+str(i[2]).center(15," ")+"\n")76 else: #将余额和消费总额写入77 f_buy.writelines(str(i[0]).ljust(30, " ") + str(i[1]).center(15, " ") + "\n")78 79 #打印最终购买清单80 print("This is your purchase list".center(45,"-"))81 with open("shopping_list","r") as f_read:82 for i in f_read:83 print(i.strip("\n"))
购买清单文件
----product---------price----------count----- pic 2 20 rice 3 50 water 32 300 phone 5000 2 pia 300 50 phone 5000 2 phone 5000 1 Total_Consumption: 49790 Your balance: 210