我试图通过制作一个对象列表来制作一个接收程序。为此,我创建了一个类和函数,如:
class ware(object):
def __init__(self, code, name, price, quantity):
self.code= code
self.name = name
self.price = price
self.quantity= quantity
def code (self):
return self.code
def my_wares():
wares_from_file= open("wares.txt", "r")
list_of_wares= []
for line in wares_from file:
every_ware1= line
every_ware2= varje_vara1.split("/")
wares= ware(every_ware2[0],every_ware2[1], every_ware2[2], every_ware2[3])
list_of_wares.append(varorna)
list_of_wares.sort()
return(list_of_wares)
完成对象列表后,通过以下方式调用main函数中的函数:
def main():
all_wares = my_wares()
现在我需要在另一个类方法中使用这个ware对象列表,我尝试了:
class receipt_part(ware):
def __init__(self, kod, namn, pris, butik_antal):
ware.__init__(self, code, name, price, quantity)
self.price_toal= 0
self.customer_quantity= 0
def shop_ware(kod):
wares_quantity= input("how many wares does the customer wants?")
self.name= all_wares[1]
self.price= all_wares[2]
peice_total+= all_wares[3]*wares_quantity
customer_quantity+= int(wares_quantity)
quantity= int(all_wares[4]) - int(wares_quantity)
__str__()
但是它说所有的产品都没有定义,虽然我在我的主要功能中使用了它,为什么呢?你知道吗
不能在一个函数中定义变量并在另一个函数中使用它,即使第一个函数被调用
main
。您需要显式地将变量传递给第二个函数。你知道吗相关问题 更多 >
编程相关推荐