print(object.method) not printing expected output?
up vote
0
down vote
favorite
I'm having a foray into OOP in python,
This project is creating some randomly generated RPG characters
The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats.
Here is how the characters are randomly generated:
def generateCharacters():
classes = ["B", "E", "W", "D", "K"]
choice = random.choice(classes)
if choice == "B":
return barbarian(70, 20, 50)
elif choice == "E":
return elf(30, 60, 10)
elif choice == "W":
return wizard(50, 70, 30)
elif choice == "D":
return dragon(90, 40, 50)
elif choice == "K":
return knight(60, 10, 60)
and here is the barbarian class, all the other classes are more or less identical:
class barbarian(character):
def __init__(self, charPower, charSAttackPwr, charSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "B", 100)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack
Power: %s, Speed: %s" % (self.name, self.type, self.health,
self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
I've created a method called getStats, which using string concatenation to make a string that shows all the stats:
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
When I run the code, it calls main(), which in turn calls menu():
def menu(gameChars):
print("Welcome to the RPG Character Simulator")
print("Here is your randomly generated team: ")
for x in gameChars:
print(x.getStats)
def main():
gameChars =
for x in range(10):
y = generateCharacters()
gameChars.insert(x, y)
#z = generateCharacters()
menu(gameChars)
#print(z.getStats)
The output I was expecting from the print(x.getStats) would've been, using examples:
Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20
but instead, I get this:
<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>
What am I missing with this? and how could I get the intended output?
Thanks in advance for any help
python oop
add a comment |
up vote
0
down vote
favorite
I'm having a foray into OOP in python,
This project is creating some randomly generated RPG characters
The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats.
Here is how the characters are randomly generated:
def generateCharacters():
classes = ["B", "E", "W", "D", "K"]
choice = random.choice(classes)
if choice == "B":
return barbarian(70, 20, 50)
elif choice == "E":
return elf(30, 60, 10)
elif choice == "W":
return wizard(50, 70, 30)
elif choice == "D":
return dragon(90, 40, 50)
elif choice == "K":
return knight(60, 10, 60)
and here is the barbarian class, all the other classes are more or less identical:
class barbarian(character):
def __init__(self, charPower, charSAttackPwr, charSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "B", 100)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack
Power: %s, Speed: %s" % (self.name, self.type, self.health,
self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
I've created a method called getStats, which using string concatenation to make a string that shows all the stats:
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
When I run the code, it calls main(), which in turn calls menu():
def menu(gameChars):
print("Welcome to the RPG Character Simulator")
print("Here is your randomly generated team: ")
for x in gameChars:
print(x.getStats)
def main():
gameChars =
for x in range(10):
y = generateCharacters()
gameChars.insert(x, y)
#z = generateCharacters()
menu(gameChars)
#print(z.getStats)
The output I was expecting from the print(x.getStats) would've been, using examples:
Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20
but instead, I get this:
<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>
What am I missing with this? and how could I get the intended output?
Thanks in advance for any help
python oop
1
getStats
is a function, you have to call it like any other function, sox.getStats()
– juanpa.arrivillaga
Nov 21 at 18:22
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm having a foray into OOP in python,
This project is creating some randomly generated RPG characters
The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats.
Here is how the characters are randomly generated:
def generateCharacters():
classes = ["B", "E", "W", "D", "K"]
choice = random.choice(classes)
if choice == "B":
return barbarian(70, 20, 50)
elif choice == "E":
return elf(30, 60, 10)
elif choice == "W":
return wizard(50, 70, 30)
elif choice == "D":
return dragon(90, 40, 50)
elif choice == "K":
return knight(60, 10, 60)
and here is the barbarian class, all the other classes are more or less identical:
class barbarian(character):
def __init__(self, charPower, charSAttackPwr, charSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "B", 100)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack
Power: %s, Speed: %s" % (self.name, self.type, self.health,
self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
I've created a method called getStats, which using string concatenation to make a string that shows all the stats:
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
When I run the code, it calls main(), which in turn calls menu():
def menu(gameChars):
print("Welcome to the RPG Character Simulator")
print("Here is your randomly generated team: ")
for x in gameChars:
print(x.getStats)
def main():
gameChars =
for x in range(10):
y = generateCharacters()
gameChars.insert(x, y)
#z = generateCharacters()
menu(gameChars)
#print(z.getStats)
The output I was expecting from the print(x.getStats) would've been, using examples:
Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20
but instead, I get this:
<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>
What am I missing with this? and how could I get the intended output?
Thanks in advance for any help
python oop
I'm having a foray into OOP in python,
This project is creating some randomly generated RPG characters
The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats.
Here is how the characters are randomly generated:
def generateCharacters():
classes = ["B", "E", "W", "D", "K"]
choice = random.choice(classes)
if choice == "B":
return barbarian(70, 20, 50)
elif choice == "E":
return elf(30, 60, 10)
elif choice == "W":
return wizard(50, 70, 30)
elif choice == "D":
return dragon(90, 40, 50)
elif choice == "K":
return knight(60, 10, 60)
and here is the barbarian class, all the other classes are more or less identical:
class barbarian(character):
def __init__(self, charPower, charSAttackPwr, charSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "B", 100)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack
Power: %s, Speed: %s" % (self.name, self.type, self.health,
self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
I've created a method called getStats, which using string concatenation to make a string that shows all the stats:
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
When I run the code, it calls main(), which in turn calls menu():
def menu(gameChars):
print("Welcome to the RPG Character Simulator")
print("Here is your randomly generated team: ")
for x in gameChars:
print(x.getStats)
def main():
gameChars =
for x in range(10):
y = generateCharacters()
gameChars.insert(x, y)
#z = generateCharacters()
menu(gameChars)
#print(z.getStats)
The output I was expecting from the print(x.getStats) would've been, using examples:
Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20
but instead, I get this:
<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>
What am I missing with this? and how could I get the intended output?
Thanks in advance for any help
python oop
python oop
asked Nov 21 at 18:20
LegitShellAccount
153
153
1
getStats
is a function, you have to call it like any other function, sox.getStats()
– juanpa.arrivillaga
Nov 21 at 18:22
add a comment |
1
getStats
is a function, you have to call it like any other function, sox.getStats()
– juanpa.arrivillaga
Nov 21 at 18:22
1
1
getStats
is a function, you have to call it like any other function, so x.getStats()
– juanpa.arrivillaga
Nov 21 at 18:22
getStats
is a function, you have to call it like any other function, so x.getStats()
– juanpa.arrivillaga
Nov 21 at 18:22
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Replace this :
print(x.getStats)
With this :
print(x.getStats())
Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)
– LegitShellAccount
Nov 21 at 18:35
add a comment |
up vote
0
down vote
another version would be to use the @property
decorator:
class Barbarian(Character):
@property
def getStats(self):
return 'Name: {.name}'.format(self)
which would allow:
bob = Barbarian('bob', …)
print(bob.getStats)
to work as you expect
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Replace this :
print(x.getStats)
With this :
print(x.getStats())
Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)
– LegitShellAccount
Nov 21 at 18:35
add a comment |
up vote
0
down vote
accepted
Replace this :
print(x.getStats)
With this :
print(x.getStats())
Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)
– LegitShellAccount
Nov 21 at 18:35
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Replace this :
print(x.getStats)
With this :
print(x.getStats())
Replace this :
print(x.getStats)
With this :
print(x.getStats())
answered Nov 21 at 18:24
Amine Messaoudi
450314
450314
Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)
– LegitShellAccount
Nov 21 at 18:35
add a comment |
Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)
– LegitShellAccount
Nov 21 at 18:35
Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)
– LegitShellAccount
Nov 21 at 18:35
Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)
– LegitShellAccount
Nov 21 at 18:35
add a comment |
up vote
0
down vote
another version would be to use the @property
decorator:
class Barbarian(Character):
@property
def getStats(self):
return 'Name: {.name}'.format(self)
which would allow:
bob = Barbarian('bob', …)
print(bob.getStats)
to work as you expect
add a comment |
up vote
0
down vote
another version would be to use the @property
decorator:
class Barbarian(Character):
@property
def getStats(self):
return 'Name: {.name}'.format(self)
which would allow:
bob = Barbarian('bob', …)
print(bob.getStats)
to work as you expect
add a comment |
up vote
0
down vote
up vote
0
down vote
another version would be to use the @property
decorator:
class Barbarian(Character):
@property
def getStats(self):
return 'Name: {.name}'.format(self)
which would allow:
bob = Barbarian('bob', …)
print(bob.getStats)
to work as you expect
another version would be to use the @property
decorator:
class Barbarian(Character):
@property
def getStats(self):
return 'Name: {.name}'.format(self)
which would allow:
bob = Barbarian('bob', …)
print(bob.getStats)
to work as you expect
answered Nov 21 at 18:35
Sam Mason
2,3101225
2,3101225
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418320%2fprintobject-method-not-printing-expected-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
getStats
is a function, you have to call it like any other function, sox.getStats()
– juanpa.arrivillaga
Nov 21 at 18:22