#-- coding:utf-8 --
import sys
import os
import pickle
shopfilename=‘C:\wyq\shop.txt’
shoporg={}
if not os.path.isfile(shopfilename):
with open(shopfilename,‘wb’) as fobj:
pickle.dump(shoporg,fobj)
with open(shopfilename,‘rb’) as fobj:
shopdict=pickle.load(fobj)
try:
passwd=sys.argv[1]
except IndexError:
passwd=0
if passwd==‘wang15191509’:
print(‘现有商品如下’)
for key,value in shopdict.items():
print(key,value)
pro=""“请输入您要操作的编号
1.修改商品价格
2.添加商品
3.退出系统
请选择:”""
while True:
try:
choice=input(pro)
except (EOFError,KeyboardInterrupt):
exit(‘bye-bye’)
if choice==‘1’:
try:
shopmod=input(‘请输入您要修改的商品名称:’)
price=int(input(‘请输入您要修改的商品价格:’))
except (EOFError,KeyboardInterrupt):
exit(‘bye-bye’)
except ValueError:
print(‘您的价格输入有误,请重试’)
continue
if shopdict.get(shopmod,0):
shopdict[shopmod]=price
print(‘修改成功’)
with open(shopfilename, ‘wb’) as fobj:
pickle.dump(shopdict,fobj)
else:
print(‘您输入的商品有误,请重试’)
continue
elif choice==‘2’:
try:
shopadd = input(‘请输入您要添加的商品名称:’)
price = int(input(‘请输入您要添加商品的价格:’))
except (EOFError,KeyboardInterrupt):
exit(‘bye-bye’)
except ValueError:
print(‘您输入的价格有误,请重试!!!’)
continue
if shopadd:
shopdict[shopadd]=price
with open(shopfilename, ‘wb’) as fobj:
pickle.dump(shopdict,fobj)
for key,value in shopdict.items():
print(key,value)
print(’*’*10)
print(‘添加成功’)
else:
print(‘您还没有输入商品名称,请重试!!!’)
continue
elif choice==‘3’:
exit(‘bye-bye’)
else:
print(‘您的输入有误,请重试!!!’)
continue
else:
print(‘商品如下’)
print(’%-10s%-10s%s’ % (‘编号’,‘名称’,‘价格’))
with open(shopfilename,‘rb’) as fobj:
shopdict=pickle.load(fobj)
shoplist=list(shopdict.items())
for i in shoplist:
print(’%-10s%-10s%s’ % (shoplist.index(i),i[0],i[1]))
while True:
try:
choice=int(input(‘请输入您要购买的商品编号’))
except ValueError:
print(‘编号输入有误,请重试!!!’)
continue
except (EOFError,KeyboardInterrupt):
exit(‘bye-bye’)
if not choice in [shoplist.index(i) for i in shoplist]:
print(‘编号输入有误,请重试!!!’)
continue
else:
print(‘购买成功’)