我的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()
如果正确复制粘贴的代码,则
main
函数中存在明显的缩进问题相关问题 更多 >
编程相关推荐