码迷,mamicode.com
首页 > 编程语言 > 详细

python---购物车---更新

时间:2017-06-18 10:38:28      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:lin   修改   格式   put   区分   his   输入   div   序号   

购物车程序更新:

更新商家入口,实现以下功能:

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+=1
16 
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 = 0
21     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+=1
32     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+=1
46         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+=1
61         else:
62             print("Your select is invalid, Please try again!")
63 
64     elif select == "q":
65         print("You have been quit!")
66         break
67     else:
68         print("invalid input,please input again!")

 

产品清单格式如下:

pic,2
rice,3
water,32

 

python---购物车---更新

标签:lin   修改   格式   put   区分   his   输入   div   序号   

原文地址:http://www.cnblogs.com/clv5/p/7042643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!