商家修改增加删除商品,金额 个人购买程雪



# 用户类型存放列表(商家,个人)
new_item = []
# shop商品信息列表
shop = []
# 定义一个商品信息存放表
new_shop_list = []
# 存放index,item的列表
shop_ws = []
# 将商品信息(商品,价格).txt 读到列表shop里面去
with open("商品信息.txt ", 'r+') as op:
shop_list = op.readlines()
for _shop_list in shop_list:
shop.append(_shop_list.strip('\n').split(','))
#print(len(shop))
print(shop)

# 登陆用的字典info
info = {'商家':
['新增商品',
'修改商品',
'删除商品'],
'个人': []
}
#print(info)
# 使用下标做标记位,用enumerate把shop的下角标读到index,shop读到item
for index, item in enumerate(shop):
print(index, item)
shop_ws.append(index)
shop_ws.append(item)
# 自己定义一个true对象,在while true循环中代替break
wangsen = True
while wangsen:
print('\033[32;40m------欢迎来到购物系统!!!---------\033[0m')
for items in info:
# 打印出两个绿底的********,中间的item是变量(商家,个人)
print('\033[32;1m*******\033[0m', items, '\033[32;1m*******\033[0m')
# 在底部打印出退出程序的位置
print('\033[32;1m*****', '退出程序请按:q', '\033[32;1m*****\033[0m')
# 定义一个select 变量 为了进入菜单做判断
select = input('请输入您的身份:')
# 如果输入q 结束循环
if select == 'q':
print('欢迎下次使用')
wangsen = False
else:
# 如果select输入存在在info字典里
if select in info:
while wangsen:
if select == '个人':
print('\033[32;1m*****欢迎来到王森购物平台*********\033[0m')
print('------------商品列表如下----------------')
print(shop_ws)
salary = input('请输入您的工资:')
# 判断工资为是否为数字,并将salary赋值为整数型
if salary.isdigit():
salary = int(salary)
while wangsen:
# 输入商品id
user_choice = input('请输入商品id:')
# 判断商品id是否为数字,并将user_choice赋值为整数型
if user_choice.isdigit():
user_choice = int(user_choice)
# 判断user_choice存在
if user_choice < len(shop) and user_choice >= 0:
# p_item就是存放商品信息的地方(商品名称,金额)
p_item = shop[user_choice]
print(p_item)
# 如果 商品金额小于 工资
if salary >= int(p_item[1]):
new_shop_list.append(p_item)
# 就把商品信息加到shop列表里面去
# 把工资扣除
salary -= int(p_item[1])
print("你买了{info_1},余额\033[31;1m{info_2}\033[0m".format(
info_1=new_shop_list, info_2=salary))


else:
print('钱不够啦')
wangsen = False

else:
print('商品id不存在')
wangsen = False

else:
print('您输入的商品id有误,必须为存在的商品编号')
wangsen = False

else:
print('您输入的工资有误')
wangsen = False
if select == '商家':
print('----------------------商家修改系统----------------------')
# 把一级目录商家的下级目录(新增商品,修改商品价格,删除商品读出来)
for business in info['商家']:
# 将下级目录打印出来
print('\033[32;1m*******\033[0m', business, '\033[32;1m*******\033[0m')
# 在底部打印出退出程序的位置
print('\033[32;1m***********\033[0m', '退出请按:q', '\033[32;1m*********\033[0m')
# 输入select_business 为了选择下级目录做判断
select_business = input('请输入您的操作:')
if select_business == 'q':
print('欢迎下次使用')
wangsen = False
if select_business == '新增商品':
print(shop_ws)
# 输入商品名称(commodity),商品价格 ,写进商品信息.txt
commodity = input('请输入商品名称:')
Price = input('请输入商品价格:')
with open('商品信息.txt', 'a') as op:
#{ws1},{ws2}后面如果加上\n就能换行了重点 这有个空格
op.write("{ws1},{ws2} \n".format(ws1=commodity, ws2=Price))
print('\033[32;1m******\033[0m', '商品添加成功', '\033[32;1m*****\033[0m')
if select_business == '修改商品':
print('\033[32;1m******\033[0m', '请对下列商品进行修改', '\033[32;1m*****\033[0m')
print(shop_ws)
pro_number = input('\033[38;1m请输入要修改商品的编号:\033[0m')
if pro_number.isdigit():
pro_number = int(pro_number)
new_price = input('请输入要修改的价格:')
#shop相当于一个二维数组
shop[pro_number][1]=new_price
#把这个txt文件删除
os.remove('商品信息.txt')
#运用for循环把shop中的内容读进new_message ,在写进被清空的 商品信息.txt
#a,追加模式。【可读; 不存在则创建;存在则只追加内容;】
for new_message in shop:
with open ('商品信息.txt','a') as op:
op.write('{info1},{info2} \n'.format(info1=new_message[0],info2=new_message[1]))
print('修改成功')
else:
print('您输入的商品id错误')
if select_business == '删除商品':
print('\033[32;1m**********\033[0m','请对以下产品进行删除','\033[32;1m***********\033[0m')
print(shop_ws)
del_number = input('请输入删除商品的商品id:')
if del_number.isdigit():
del_number = int(del_number)
#删除对应的商品编号
del shop[del_number]

#删除文件,用os模块的remove
os.remove('商品信息.txt')
#将现在shop的信息for循环到new_del_shop中
for new_del_shop in shop:
with open('商品信息.txt','a') as op:
op.write('{ws1},{ws2} \n'.format(ws1=new_del_shop[0],ws2=new_del_shop[1]))
print('\033[32;1m******\033[0m','删除成功','\033[32;1m*******\033[0m')

else:
print('商品id输入错误')
posted @ 2016-08-02 22:39  wangsen123  阅读(246)  评论(0编辑  收藏  举报