名称storeProducts中项目的错误行50,未定义名称“storeProducts”

2024-06-17 15:33:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python程序有一个问题,我试图创建一个类来保存零售商店中某个商品的数据。我不知道为什么它给了我名字。另外,是否可以将storeProducts列表中的数据转换为字典,并仍将其显示为表

class Products:
    def __init__(self,productId,description,quantity,price):
        self.productId = productId
        self.description = description
        self.quantity = quantity
        self.price = price

    def set_productId(self,productId):
        self.__productId = productId

    def set_description(self,description):
        self.__description = description

    def set_quantity(self,quantity):
        self.__quantity = quantity


    def set_price(self,price):
        self.__price = price

    def get_productId(self,productId):
        return self.__productId 

    def get_description(self,description):
        return self.__description

    def get_quantity(self,quantity):
        return self.__quantity

    def get_price(self,price):
        return self.__price

    def __str__(self):
        return'Products:'+ 'title:' + self.title+', description:' + self.description + \
            ' ,quantity' + self.quantity + ', price:' + self.price

def main():
    storeProducts = [[1,'Jacket',1,59.95],
                 [2, 'Designer Jeans' , 40, 34.95],
                 [3, 'Shirt' , 20, 24.95],]
print=(': Product ID : Description : Quantity : Price : ')

for item in storeProducts:
    print(':',item[0],''*(9-len(str(item[0]))), ':',
          item[1],''*(11-len(item[1])),':',
          item[2],''*(8-len(str(item[2]))),':',
          item[3],''*(5-len(str(item[3]))))


main()

Tags: 数据selfgetlenreturndefdescriptionitem