购物车程序优化(python3.x)

Posted summer-future

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车程序优化(python3.x)相关的知识,希望对你有一定的参考价值。

针对上次的程序进行了如下优化:

    1.整个程序分为两个接口,分别为商家可用户

  2.商家可以添加商品,修改价格

  3.商品信息存储在文件中,余额和已购商品也存在文件中

 

 

 1 products_list=[]
 2 shopping_list=[]
 3 price_list=[]
 4 while True:
 5     role=input("请选择你以什么样的身份进入系统 1.user| 2.ower:")
 6     if role==1:
 7         print ("欢迎使用user身份进入系统")
 8         with open("code_file/product_info",r) as h:
 9             f=len(h.readlines() )
10         with open("code_file/product_info",r) as u:
11             for i in range(f):
12                 single_produce=u.readline().strip(
)
13                 products_list.append(single_produce)
14         with open("code_file/price_info",r) as m:
15             for i in range(f):
16                 single_price=int(m.readline().strip(
))
17                 price_list.append(single_price)
18         salary=input("input your salary:")
19         if salary.isdigit():
20             salary=int(salary)
21             while True:
22                 for index,item in enumerate (products_list ):
23                     print(index,item,price_list[index] )
24                 choose_num=input("plese your choose...:")
25                 if choose_num.isdigit():
26                     choose_num=int(choose_num )
27                     if choose_num<len(products_list) and choose_num >=0:
28                         if salary>=price_list[choose_num]:
29                             shopping_list.append(products_list[choose_num])
30                             p=products_list[choose_num]
31                             salary-=price_list[choose_num]
32                             print("added 33[31;1m%s33[0m into your shopcar...,your balance is 33[31;1m%s33[0m" % (p,salary ))
33                         else:
34                             print("your salary can not afford it....")
35                     else:
36                         print("now,your choose is false....")
37                 elif choose_num=="q":
38                     with open("code_file/bought_info",a+) as bought:
39                         for bought_products in shopping_list :
40                             bought.writelines(bought_products)
41                             bought.writelines(
)
42                             print (bought_products )
43                     with open("code_file/balance_info",w) as balance:
44                         balance.writelines(str(salary))
45                         balance.writelines(
)
46                     print("your balance is 33[31;1m%s33[0m"%salary)
47                     exit()
48                 elif choose_num==b:
49                     break
50                 else:
51                     print("your input is false....,plese input agin...")
52 
53     elif role==2:
54         print ("欢迎你使用商家身份进入系统")
55         while True:
56             print (‘‘‘商家可以进行的操作信息      
57                 0.添加商品
58                 1.修改价格
59                 2.读取商品的信息
60                     ‘‘‘)
61             next=input("请输入你的选择:")
62             if next.isdigit():
63                 next=int(next)
64             if next==0:
65                 join_product=input("请输入添加的商品名称:")
66                 join_price=input("请输入添加的商品价格:")
67                 with open("code_file/product_info",a+) as o:
68                     o.writelines(join_product)
69                     o.writelines(
)
70                 with open("code_file/price_info",a+) as a:
71                     a.writelines(join_price)
72                     a.writelines(
)
73                     continue
74             elif next==1:
75                 change_name=input("请输入需要修改的商品名称:")+

76                 change_price=input("请输入该商品修改为多钱:")+

77                 with open("code_file/price_info",r+)as o:
78                     read_price=o.readlines()
79                     print(read_price)
80                     read_price[products_list.index(change_name)] =change_price
81                     print(read_price)
82                 with open("code_file/price_info",w+)as q:
83                     q.writelines(read_price)
84             elif next==2:
85                 with open("code_file/product_info",r)as b:
86                     products_list =b.readlines()
87                     for index,item in enumerate (products_list ):
88                         print(index,item)
89             elif next==b:
90                 break
91             elif next==q:
92                 exit()
93             else:
94                 print("输入错误")
95     elif role==q:
96         exit()
97     else:
98         print("输入异常")

 

以上是关于购物车程序优化(python3.x)的主要内容,如果未能解决你的问题,请参考以下文章

三级菜单购物车程序(自写,待优化)

Python2.x和Python3.x的区别

39 生产案例:基于延迟消息机制优化大量订单的定时退款扫描问题示例代码

python购物车程序的简单程序优化版

购物商城小程序

Python3.x和Python2.x的区别