将变量附加到另一个函数的列表/调用列表

2024-03-29 11:46:45 发布

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

因此,我修复了以前基于没有正确定义列表的问题。是否可以组织列表中的项目?或者它总是显示它被添加的方式?如果在创建并添加到列表后有方法更新列表,如何更新?你知道吗

这是主类文件:

 def storing():  

    the_item = Ch10b_retailItemClass.RetailItem("","","")

    for count in range(3):  

        desc = input('What item would you like to inventory?\n')
        unit = input('How many of them are in stock?\n')
        price = input('What\'s the price for this item?\n') 

        the_item.set__desc(desc)
        the_item.set__unit(unit)
        the_item.set__price(price)

    # I want to print the list as a whole.
    print(the_item.list_function())

这是我要导入的类:

class RetailItem:
    global inv
    inv = []
# init function, getting us started and initializing variables?
    def __init__(self, desc, unit, price):
        self.__desc = desc
        self.__unit = unit
        self.__price = price

    # functions that create temp place holders
    def set__desc(self, desc):
        self.__desc = desc
        inv.append(desc)

    def set__unit(self, unit):
        self.__unit = unit
        inv.append(unit)

    def set__price(self, price):
        self.__price = price
        inv.append(price)

    def list_function(self):
        return inv

Tags: theself列表inputdefunitfunctionitem