【阶段性成果3-1】Python编写购物车程序优化-商家入口

需求:

'''
1.商品信息存在文件里
2.可以实现增删改
'''
'''
文件存储格式:
iPhoneXR 6299
XiaoMi9 3299
丰田威驰 79800
Only春装 1199
佛跳墙 999
苹果 333
西瓜 111
荔枝 555
桂圆 888
橘子 666
菠萝 121
草莓 212
'''


 

正文:

 1 # Author:David Liu 
 2 # Be Happy!
 3 dict={}
 4 file = open('C:/Users/David/Desktop/商品列表.txt').readlines()
 5 for i in file:
 6     print(i.strip())
 7 res=True
 8 while res:
 9     que=input("是否作出修改>>(y/n)")
10     if que=='y':
11         que2=input("添加商品>>a,修改价格>>p,删除商品>>d")
12         if que2=='a':
13             while True:
14                 name = input("输入商品名称>>")
15                 price = input("输入商品价格>>")
16                 f = open('C:/Users/David/Desktop/商品列表.txt', 'a')
17                 f.write('\n')
18                 f.write(name)
19                 f.write(' ')
20                 f.write(price)
21                 print("添加成功!")
22                 f.close()
23                 f = open('C:/Users/David/Desktop/商品列表.txt', 'r')
24                 for j in f.readlines():
25                     print(j.strip())
26                 a=input("是否继续添加>>(y/n)")
27                 if a=='y':
28                     f.close()
29                     continue
30                 else:
31                    f.close()
32                    break
33         elif que2=='p':
34             f = open('C:/Users/David/Desktop/商品列表.txt', 'r')
35             for i in f.readlines():
36                 (key, value) = i.strip().split(' ')  # 切片,再存入字典
37                 dict.setdefault(key, value)
38             print(dict.keys())  # 打印key
39             while True:
40                 f = open('C:/Users/David/Desktop/商品列表.txt', 'w')
41                 goods = input("请输入商品名>>")
42                 if goods in dict:
43                     print("原价为:", dict.get(goods))
44                     price_new = input("输入更新价格>>")
45                     dict[goods] = price_new
46                     print("修改成功,信息更新!")
47                     for key in dict:
48                         print(key, dict[key])
49                         f.write(key)
50                         f.write(' ')
51                         f.write(dict[key])
52                         f.write('\n')
53                     sig = input("是否继续修改>>(y/n)")
54                     if sig == 'y':
55                         f.close()
56                         continue
57                     else:
58                         f.close()
59                         res == False
60                         break
61                 else:
62                     print("输入错误!")
63                     continue
64         elif que2=='d':
65             f = open('C:/Users/David/Desktop/商品列表.txt', 'r+')
66             for i in f.readlines():
67                 (key, value) = i.strip().split(' ')  # 切片,再存入字典
68                 dict.setdefault(key, value)
69             print(dict.keys())  # 打印key
70             while True:
71                 f2 = open('C:/Users/David/Desktop/商品列表.txt', 'w')
72                 goods = input("请输入要删除的商品>>")
73                 if goods in dict:
74                     del dict[goods]
75                     print("删除成功,信息更新!")
76                     for key in dict:
77                         print(key, dict[key])
78                         f2.write(key)
79                         f2.write(' ')
80                         f2.write(dict[key])
81                         f2.write('\n')
82                     sig = input("是否继续删除>>(y/n)")
83                     if sig == 'y':
84                         f2.close()
85                         continue
86                     else:
87                         f2.close()
88                         break
89                 else:
90                     print("输入错误!")
91                     continue
92     else:
93         print('\n', "已退出!".center(50, '-'))
94         break

结语:

目前为止最长代码,功能基本实现,全部为自己所想所写!

程序所用数据皆为虚构,仅供实验所用。

2019年3月7日

posted @ 2019-03-07 20:41  David0207  阅读(251)  评论(0编辑  收藏  举报