小编G. *_*ews的帖子

在 Python 中更改另一个类中的属性

我一直在尝试从类 Pod 更改类 Inventory 中的列表,但是我收到一个错误,提示我从一个空集合中弹出。无论如何,我可以从我知道已填充的 Inventory 实例的列表中弹出吗?本质上,我正在尝试将小部件从 Inventory 转移到 Pod。

class Widget():

    def __init__(self):
        self.cost = 6
        self.value = 9


class Inventory():

    def __init__(self):
        self.widgets_inv = []
        self.cost_inv = 0
        self.value_inv = 0

    def buy_inv(self):
        x = int(input("How many widgets to you want to add to inventory? "))
        for i in range(0, x):
            self.widgets_inv.append(Widget())

    def get_inv(self):
        print("You have " + str(len(self.widgets_inv)) + " widgets in inventory.")

    def cost_of_inv(self):
        cost_inv = len(self.widgets_inv) * Widget().cost
        print("The current cost of your inventory is: …
Run Code Online (Sandbox Code Playgroud)

attributes class python-3.x

4
推荐指数
1
解决办法
844
查看次数

标签 统计

attributes ×1

class ×1

python-3.x ×1