0

I'm having trouble with trying to print the stats of the player in a class function. When I run it, it doesn't print.

class Player(object):
    def __init__(self, name, hp, dmg, lvl, inventory, speed):
       self.name = name
       self.hp = hp
       self.dmg = dmg
       self.lvl = lvl
       self.inventory = inventory
       self.speed = speed

    def printStats(self):
       return("Health: " + self.hp + ", Attack Damage: " + self.dmg + ", Level: " + self.lvl + ", Speed: " + self.speed)

link = Player("Link", 10, 3, 1, {}, 3)
link.printStats
1
  • You don't call the function... Commented Dec 18, 2015 at 20:10

4 Answers 4

4

If you want to print stats, you must print the stats.

def printStats(self):
    print("Health: " + ...)

You also need to actually call the function (notice the trailing parenthesis):

link.printStats()

A somewhat more pythonic solution would be to modify what gets printed when you print the object. You can do that by defining the method __str__. Then, just pass your object to the print command.

class Player(object):
    def __init__(self, name, hp, dmg, lvl, inventory, speed):
       self.name = name
       self.hp = hp
       self.dmg = dmg
       self.lvl = lvl
       self.inventory = inventory
       self.speed = speed

    def __str__(self):
        return "Health: %s Attack Damage: %s Level: %s Speed: %s" % (self.hp, self.dmg, self.lvl, self.speed)

link = Player("Link", 10, 3, 1, {}, 3)
print(link)
Sign up to request clarification or add additional context in comments.

Comments

0

Instead you can use __repr__(self) function:

class Player(object):
    def __init__(self, name, hp, dmg, lvl, inventory, speed):
        self.name = name
        self.hp = hp
        self.dmg = dmg
        self.lvl = lvl
        self.inventory = inventory
        self.speed = speed

    def __repr__(self):
         return("Health: " + self.hp + ", Attack Damage: " + self.dmg + ", Level: " + self.lvl + ", Speed: " + self.speed)


link = Player("Link", 10, 3, 1, {}, 3)
print link

Comments

0

When you run a program in IDLE or the REPL, python automatically prints the return value of the function. So replace this:

def printStats(self):
    return("Health: " + ...)

with this:

def printStats(self):
    print("Health: " + ...)

You also failed to actually call the function (i.e. run).

link.printStats

simply says "Ok, find me the printStats function from link."

You didn't run it, you simply retrieved it.

Try this instead:

link.printStats()

BTW, python convention says that you should not use CamelCase lettering for functions. Use name_with_underscores instead.

Comments

0

depending on what version of python you are running in you'll need to do this

for python 2.7

    print link.printStats()

for python 3.x

    print(link.printStats())

2 Comments

unless you actually call the function, nothing is going to get printed or returned.
@VarunTewari Yes, but that's not what you have.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.