报错内容Traceback (most recent call last):
File "C:/Users/heliun/PycharmProjects/pythonProject/14.py", line 117, in
supermarket.mainmenu()
File "C:/Users/heliun/PycharmProjects/pythonProject/14.py", line 86, in mainmenu
goods = Goods(id, name, price, num,allprice)
UnboundLocalError: local variable 'allprice' referenced before assignment
具体代码:class Goods:
def init(self,id,name,price,num):
self.id=id
self.name=name
self.price=price
self.num=num
class MySuperMarket:
def init(self,id,name):
self.id=id
self.name=name
self.allingoods=[]
self.alloutgoods=[]
self.allinvent={}
def ingoods(self, goods):
dict = {}
dict[goods.id] = goods
self.allingoods.append(dict)
if goods.id in self.allinvent.keys():
object = self.allinvent[goods.id]
object.price = (object.price * object.num + goods.price * goods.num) / (object.num + goods.num)
object.num = object.num + goods.num
self.allinvent[object.id] =object
return 1
inventgoods=Goods(goods.id,goods.name,goods.price,goods.num)
self.allinvent[inventgoods.id] = inventgoods
def outgoods(self,goods):
dict={}
dict[goods.id]=goods
self.alloutgoods.append(dict)
if goods.id in self.allinvent.keys():
if goods.num<=object.num:
object.num=object.num-goods.num
self.allinvent[object.id]=object
else :
print("库存数量不足!清重新输入")
goods.num=int(input("请重新输入商品的数量:"))
object.num = object.num - goods.num
def allinventlook(self,goods):
if goods.id in self.allinvent.keys():
self.allinvent[object.id] = object
def outlook(self,goods):
if goods.id in self.allinvent.keys():
self.allinvent[object.id] = object
def inlook(self, goods):
if goods.id in self.allinvent.keys():
self.allinvent[object.id] = object
def mainmenu(self):
while True:
print("*"*8+self.name+"*"*8)
print("1. 购进 2. 销售")
print("3. 查询 0. 退出")
print("*"*22)
sele=int(input("请输入您的选择(0:退出):"))
if sele==0:
for k in self.allingoods:
for v in k.values():
print(v.name,v.price,v.num)
for v in self.allinvent.values():
print(v.name,v.price,v.num,sep=" ")
print("谢谢使用...")
return 1
elif sele==1:
id=input("请输入购进的商品ID:")
name=input("请输入购进的商品名称:")
price=float(input("请输入商品单价:"))
num=float(input("请输入商品数量:"))
goods = Goods(id,name, price,num)
self.ingoods(goods)
elif sele==2:
id=input("请输入销售商品的ID:")
name=input("请输入销售商品的名称:")
num= int(input("请输入销售商品的数量:"))
price= int(input("请输入销售商品的单价:"))
for a in self.alloutgoods:
for b in i.values():
if b.id == id:
allprice = b.price * b.num
goods = Goods(id, name, price, num,allprice)
self.outgoods(goods)
elif sele==3:
print("*" * 8 + "查找类型" + "*" * 8)
print("A. 查找库存商品 B.查找销售商品 C.查找进货商品")
print("*" * 22)
choose =input("请输入您的选择(#:退出):")
id = input("请输入需要查询的库存商品的ID:")
if choose=="A":
goods = Goods(id, name, price, num, allprice)
for c in self.allinvent:
for d in d.values():
print("名称:d.name"+"单价:d.price"+"数量:d.num")
elif choose == "B":
goods = Goods(id, name, price, num, allprice)
for e in self.allinvent:
for f in b.values():
print("名称:f.name" + "单价:f.price" + "数量:f.num"+"总价:f.allprice")
elif choose == "C":
goods = Goods(id, name, price, num)
for c in self.allinvent:
for d in d.values():
print("名称:d.name" + "单价:d.price" + "数量:d.num")
#主模块
supermarket=MySuperMarket("001","道奇超市")
supermarket.mainmenu()