Reputation:
I'm following along a Python tutorial series and I've come to classes. So.. I'm trying to make some kind of "medevial RPG class system" and are trying to fetch weapons to a warrior class. Im really new to this so would be thankful if you guys explained it as easy as possible.
So, I get an error:
AttributeError: 'Warrior' object has no attribute 'wep_name'
What am I doing wrong?
Here's the code:
class Character(object):
def __init__(self, name):
self.health = 100
self.name = name
self.equipment = {
"Weapon": 'None',
"Attack Damage": '0'
}
def printName(self):
print "Name: " + self.name
class Warrior(Character):
"""
**Warrior Class**
50% more HP
"""
def __init__(self, name):
super(Warrior, self).__init__(name)
self.health = self.health * 1.5
self.equipment["Weapon"] = self.wep_name # <-- ?
class Weapon(object):
def __init__(self, wep_name):
self.wep_name = wep_name
And sorry if the title makes no sense. I'm not really sure what this is called :(
Upvotes: 1
Views: 78
Reputation: 389
As your have no weapon member variable inside your warrior class you cannot assign it.
You will have to supply it to your init method like so
def __init__(self, name, wep_name)
like you do in the weapon class.
now you can do
self.equipment["Weapon"] = wep_name
else consider referring to an instance of the weapon class which already encapsulates the weapon_name
hope this helps you out
Upvotes: 1
Reputation: 19037
At the line containing the error, self
refers to a Warrior
, not a Weapon
, so it has no wep_name
. If you want to make a new Weapon
at that point, it might be something like:
self.equipment["Weapon"] = Weapon("Sword")
Upvotes: 2