Python Forum
Printing class results returns 'none'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing class results returns 'none'
#4
    def getStats(self):
        print(self.strength, self.dexterity, self.wisdom, self.intelligence)
It´s a bad idea to use print() to print class values in a class method.
There are two better ways:
1)
def get_stats(self):
    return f"{self.strength}, {self.dexterity}, {self.wisdom}, {self.intelligence}"
Then you can print player stats like this:
print(player.get_stats())
But please have a look at Python PEP8 code style. and name your functions and methods properly.

2)
def __str__(self):
    return f"{self.strength}, {self.dexterity}, {self.wisdom}, {self.intelligence}"
Replace your obsolete method getStats() and use the dunder-method __str__()
Then you can easily print the player stats like this
print(player)
Reply


Messages In This Thread
Printing class results returns 'none' - by Lawr3y - Aug-16-2019, 12:22 AM
RE: Printing class results returns 'none' - by ThomasL - Aug-16-2019, 01:57 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020