整体思路:
1.用户功能:购买、显示余额、列表清单、输入
2.商家功能:修改和添加商品
创建两个接口:
用户:
#Author: Gordon
#读取文档,生成goods
f = open('goods.txt',mode='r+',encoding='utf-8')
line = f.readline()
goods = eval(line)
user_goods ={}
print("商品:价格:",str(goods))
#输入工资,购买商品
salary = int(input("请输入你的工资: "))
while True:
wants = input("请输入你要购买的商品: ")
if wants in goods:
user_goods[wants] = 1
salary = salary - goods[wants]
print("你的余额还有: %d" % salary)
elif wants == 'q':
break
else:
print("对不起,我们没有这件商品")
#显示已经购买信息和余额
print("你的购物车:",str(user_goods))
print("余额:%s"% salary)
商家:
#Author: Gordon
#step1:读取文件,将商品信息保存到goods字典里
f = open('goods.txt',mode='r+',encoding='utf-8')
lines = f.readline()
print(lines)
goods = eval(lines)
print("商品数量为:%d" % len(goods))
print("商品为:" ,goods)
#step2:询问店主是要上货,还是修改商品价格,执行相应操作
info = '''
#a:添加商品
#u:修改商品价格
#q:退出
'''
print(info)
while True:
print("请问你是要添加商品或者修改商品:")
order = input("请输入命令:a or u: ")
if order == 'a':
add_goods_name = input("请输入要添加的商品名称:")
add_goods_prise = int(input("请输入添加商品的价格:"))
goods[add_goods_name] = add_goods_prise
print("添加成功")
elif order == 'u':
update_goods_name = input("请输入要修改的商品名称:")
if update_goods_name in goods :
update_goods_prise = input("请输入商品新的价格:")
goods[update_goods_name] = update_goods_prise
print("更新成功")
else:
print("对不起,你没有这件商品!")
elif order == 'q':
break
else:
print("命令错误!")
#保存进文件里
goods = str(goods)
f.seek(0,0)
f.write(goods)
f.close()
最后商城接口:
#Author: Gordon
rule = input("请输入你的角色:")
if rule == 's':
import shop
shop
elif rule == 'c':
import costomers
costomers
else:
pass
0