我如何解决属性错误?

时间:2014-04-09 13:52:00

标签: python

我们需要一种方法让我们的Tribute对象减少他们的饥饿感。创建一个方法eat(food),它接收一个Food对象。它将通过food.get food value()减少玩家的饥饿感。如果传递给eat方法的对象属于Medicine类型,则会使玩家的健康状况增加medicine.get medicine value()

预定义代码:

class Tribute(Person):
    def reduce_hunger(self, hunger):
        self.hunger = self.hunger - hunger
        if self.hunger < 0:
            self.hunger = 0
        else:
            return self.hunger

class Thing(MobileObject):
    def __init__(self, name):
        super().__init__(name, None)
        self.owner = None 

    def set_owner(self, owner):
        self.owner = owner

    def get_owner(self):
        return self.owner

    def is_owned(self):
        return self.owner is not None

class Person(LivingThing):
   def take(self, thing):
    # Can only take things in current location and not owned by others
        if isinstance(thing, Thing) and thing in self.place.objects and not thing.is_owned():
            thing.set_owner(self)
            self.inventory.append(thing)
            self.place.del_object(thing)
            GAME_LOGGER.add_event("TOOK", self, thing)
        else:
            GAME_LOGGER.warning("{} cannot take {}.".format(self.get_name(), thing.get_name()))

def named_col(col):
    # Only accepts tuple/list
    type_col = type(col)
    if type_col != list and type_col != tuple:
        return None

    return type_col(map(lambda x: x.get_name() if isinstance(x, NamedObject) else x, col))

class Food(Thing):
    def __init__(self, name, food_value):
        self.name = name
        self.food_value = food_value
    def get_food_value(self):
        return self.food_value


class Medicine(Food):
    def __init__(self, name, food_value, medicine_value):
        super().__init__(name, food_value)
        self.medicine_value = medicine_value
    def get_medicine_value(self):
        return self.medicine_value

class MobileObject(NamedObject):
    def __init__(self, name, place):
        super().__init__(name)
        self.place = place

    def get_place(self):
        return self.place

这是我的代码:

def eat(self, food):
    if food in self.get_inventory():
        self.inventory.remove(food) # not self.inventory = self.inventory.remove(food) cos self.inventory is a list. if use = it wont return anything
        self.reduce_hunger(food.get_food_value())
        if isinstance(self, medicine):
            self.health = self.health + food.get_medicine_value()
            if self.health > 100:
                self.health = 100
            elif self.health <0:
                self.health = 0

测试我的代码:

cc = Tribute("Chee Chin", 100)
chicken = Food("chicken", 5)
cc.take(chicken)                        # Chee Chin took chicken
cc.take(aloe_vera)                      # Chee Chin took aloe vera
print(named_col(cc.get_inventory()))    # ['chicken', 'aloe vera']

我收到了这个错误:

AttributeError: 'Food' object has no attribute 'owner' 

而不是预期的产量&#39; Chee Chin吃了鸡肉。

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:2)

FoodThing的子类,但Food的{​​{1}}不会调用__init__的{​​{1}}。这是问题的根源。 Thing的{​​{1}}在开始时需要以下行:

__init__