python - AttributeError: 'RangedWeapon' object has no attribute 'owner' -
in person
object, there support inventory, , when person
object takes weapon
object or food
object, object go inventory. tribute
object, want retrieve weapon
objects inventory creating new method in tribute
class, get_weapons()
, return tuple of weapon
objects tribute
has in inventory.
class tribute(person): ... def get_weapons(self): self.weapons=[] item in self.get_inventory(): if isinstance(item,weapon): self.weapons.append(item) return tuple(self.weapons) cc = tribute("chee chin", 100) chicken = food("chicken", 5) aloe_vera = medicine("aloe vera", 2, 5) bow = rangedweapon("bow", 4, 10) sword = weapon("sword", 2, 5) base = place("base") base.add_object(cc) base.add_object(chicken) base.add_object(aloe_vera) base.add_object(bow) base.add_object(sword) cc.take(bow) # chee chin took bow cc.take(sword) # chee chin took sword cc.take(chicken) # chee chin took chicken cc.take(aloe_vera) # chee chin took aloe_vera
but keep getting attributeerror: 'rangedweapon' object has no attribute 'owner'
. i'm not sure went wrong. appreciate help. thank you!
import random collections import ordereddict ###################### # class: namedobject # ###################### class namedobject(object): def __init__(self, name): self.name = name def get_name(self): return self.name ####################### # class: mobileobject # ####################### class mobileobject(namedobject): def __init__(self, name, place): super().__init__(name) self.place = place def get_place(self): return self.place ################ # class: thing # ################ 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 not none ################# # class: person # ################# class person(livingthing): def __init__(self, name, health, threshold): self.inventory = [] super().__init__(name, health, threshold) def take(self, thing): # can take things in current location , not owned others if isinstance(thing, thing) , thing in self.place.objects , 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 remove_item(self, thing): #can remove things in inventory if isinstance(thing, thing) , thing in self.get_inventory() , thing.get_owner()==self: thing.set_owner(none) self.inventory.remove(thing) else: game_logger.warning("{} not own {}.".format(self.get_name(), thing.get_name())) def go(self, direction): new_place = self.place.get_neighbor_at(direction.upper()) if new_place not none: self.move_to(new_place) else: game_logger.warning("{} cannot go {} {}".format(self.get_name(), direction, self.get_place().get_name())) def get_inventory(self): return list(self.inventory) def objects_around(self): return list(filter(lambda t: t not self, self.get_place().get_objects())) def get_exits(self): return self.get_place().get_exits()` class weapon(thing): def __init__(self, name, min_dmg, max_dmg): self.name=name self.min_dmg=min_dmg self.max_dmg=max_dmg def min_damage(self): return self.min_dmg def max_damage(self): return self.max_dmg def damage(self): return random.randint(self.min_dmg,self.max_dmg) class rangedweapon(weapon): def __init__(self, name, min_dmg, max_dmg): super().__init__(name, min_dmg, max_dmg) self.shots=0 def shots_left(self): return self.shots def load(self, ammo): if ammo.weapon_type()==self.name: self.shots+=ammo.get_quantity() ammo.remove_all() def damage(self): if self.shots==0: return 0 else: self.shots-=1 return super().damage()
not expert on python 3.x, but:
class weapon(thing): def __init__(self, name, min_dmg, max_dmg): self.name=name self.min_dmg=min_dmg self.max_dmg=max_dmg
you never set weapon inherit thing - need call super().__init__
line that's appropriate thing, in weapon's ctor. annoying python quirk.
have here - @ rangedweapon's ctor - you're doing right:
super().__init__(name, min_dmg, max_dmg)
Comments
Post a Comment