0

We need a way for our Tribute object to reduce their hunger. Create a method eat(food), which takes in a Food object. It will reduce the hunger of the player by the food.get food value(). If object that's passed to the eat method is of the type Medicine, it will increment the player’s health by medicine.get medicine value().

Pre-defined code:

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

This is my code:

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

To test my code:

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']

i got this error:

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

instead of the expected output 'Chee Chin took chicken'.

What's wrong with my code?

asked Apr 9, 2014 at 13:52
6
  • Where is Food defined? What's the rest of your error message (including line numbers and stack trace)? Commented Apr 9, 2014 at 13:58
  • And, as an extension of Kevin's line of thinking: does Food interit from Thing? Commented Apr 9, 2014 at 13:59
  • Yes it is inherited from Thing Commented Apr 9, 2014 at 14:04
  • Perhaps you should collaborate with the OP who asked this question Commented Apr 9, 2014 at 14:05
  • .food, not .food_value ? Commented Apr 9, 2014 at 14:10

1 Answer 1

2

Food is a subclass of Thing but Food's __init__ doesn't call Thing's __init__. That is the source of the problem. Food's __init__ needs the following line at its start:

super().__init__(name)
answered Apr 9, 2014 at 14:39
Sign up to request clarification or add additional context in comments.

2 Comments

Furthermore, now that the superclass __init__() is being called, the self.name = name line is no longer needed in Food.
You use super() when you need to initialize variables that exist in a parent class. "Okay, so how do I know when I need to do that?", you might ask. Easy! In every subclass you create. Basically, super().__init__() should be the first executable line in any subclass' __init__(). (Just pass in the variables that need to be sent to the parent. In this case, name.) I would suggest you bookmark this post on Python inheritance and refer to it periodically.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.