# @Author Jonathon
# @Software: PyCharm Community Edition
# @time:2017/10/2915:53
import json
new_product={}
mod_product={}
data={}
while True:
selected=input('add:添加商品,mod:修改价格,q:退出')
if selected=='add':
name=input('请输入商品名称')
price=input('请输入商品价格')
new_product[name]=price
with open('D:/products.txt','a') as productfile:
productfile.write('\n'+str(new_product)) #还有一种写法:print(str,file=productfile
if selected=='q':
exit()
if selected=='mod':
with open('D:/products.txt','r') as read_file:
for line in read_file:
line=line.strip('\n')
k,v=line.split(':')
data[k]=v
print(data)
mod_selected=input('请选择要修改的商品名称')
if mod_selected in data: #判断元素是否在字典的key中
price_mod=input('请输入修改的价格')
if price_mod.isdigit():
data[mod_selected]=price_mod
with open('D:/products.txt','w') as mod_file:
for (k1,v1) in data.items():
mod_file.write('%s:%s'%(k1,v1))
mod_file.write('\n')
修改商品价格
最新推荐文章于 2024-04-22 16:07:30 发布