Reputation: 3
Working on a text game, I ran into this error where playercharacter
isn't assigning any of the variables I've given it. An example run is as follows:
What is your name?
Ty
playercharacter.getname()
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
playercharacter.getname()
AttributeError: 'player' object has no attribute 'getname'
No matter which attribute I try to call, it returns the same error message.
What am I doing wrong here?
Here's the full code below:
class player(object):
def __init__(self, name, level, ST, PE, EN, CH, IN, AG, LK, location, HP, XP, NXTLVL):
self.name = name
self.level = level
self.ST = ST
self.PE = PE
self.EN = EN
self.CH = CH
self.IN = IN
self.AG = AG
self.LK = LK
self.smallguns = 2 * AG + LK
self.bigguns = 2 * ST + LK
self.unarmed = 2 * EN + LK
self.meleeweapons = 2 * ST + LK
self.throwing = 2 * AG + LK
self.firstaid = 2 * IN + LK
self.sneak = 2 * AG + LK
self.lockpick = 2 * PE + LK
self.steal = 2 * AG + LK
self.science = 2 * IN + LK
self.repair = 2 * IN + LK
self.speech = 2 * CH + LK
self.location = location
self.HP = HP
self.XP = XP
self.NXTLVL = NXTLVL
def getname(self):
return self.name
def getlevel(self):
return self.level
def getST(self):
return self.ST
def getPE(self):
return self.PE
def getEN(self):
return self.EN
def getCH(self):
return self.CH
def getIN(self):
return self.IN
def getAG(self):
return self.AG
def getLK(self):
return self.LK
def getsmallguns(self):
return self.smallguns
def getbigguns(self):
return self.bigguns
def getunarmed(self):
return self.unarmed
def getmeleeweapons(self):
return self.meleeweapons
def getthrowing(self):
return self.throwing
def getfirstaid(self):
return self.firstaid
def getsneak(self):
return self.sneak
def getlockpick(self):
return self.lockpick
def getsteal(self):
return self.steal
def getscience(self):
return self.science
def getrepair(self):
return self.repair
def getspeech(self):
return self.speech
def getlocation(self):
return self.location
def getHP(self):
return self.HP
def getXP(self):
return self.XP
def getNXTLVL(self):
return self.NXTLVL
def __str__(self):
print"""
Name: %s
Level: %s
XP: %s
Nxt Lvl: %s
Location: %s
HP: %s
ST: %s
PE: %s
EN: %s
CH: %s
IN: %s
AG: %s
LK: %s
Small Guns: %s
Big Guns: %s
Unarmed: %s
Melee Weapons: %s
Throwing: %s
First-Aid: %s
Sneak: %s
Lockpick: %s
Steal: %s
Science: %s
Repair: %s
Speech: %s
""" %(self.getname(), self.getlevel(), self.getXP(), self.getNXTLVL(), self.getlocation(), self.getHP(), self.getST(), self.getPE(), self.getEN(), self.getCH(), self.getIN(), self.getAG(), self.getLK(), self.getsmallguns(), self.getbigguns(), self.getunarmed(), self.getmeleeweapons(), self.getthrowing(), self.getfirstaid(), self.getsneak(), self.getlockpick(), self.getsteal(), self.getscience(), self.getrepair(), self.getspeech())
player_location = {'player_grid_height': 1, 'player_grid_length': 1}
playercharacter = player(raw_input("""What is your name?
"""), 1, 5, 5, 5, 5, 5, 5, 5, player_location, 1, 0, 50)
playercharacter.__str__()
Also, any general advice would be great, thanks!
Upvotes: 0
Views: 53
Reputation: 155403
You overindented your other methods; all the def get*
names should be at the same indentation level as def __init__
, not the level of __init__
's contents.
As far as general advice goes: Don't write Java-style accessors. The Pythonic approach is to just access the attributes directly, or if you insist on some protection, naming the instance attributes with a leading underscore, and using @property
accessors. So if name
should be protected against mutation, you'd have:
def __init__(self, ...):
self._name = ...
@property
def name(self):
return self._name
so use of the property remains myobj.name
, with no need to explicitly call a function or use superfluous get
prefixes.
Upvotes: 2