Printing class results returns 'none' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Printing class results returns 'none' (/thread-20526.html) |
Printing class results returns 'none' - Lawr3y - Aug-16-2019 Hi, I've recently got into learning the Python language using Python 3.7. I'm using the base of a text rpg to sort of learn the ropes of Python since it's something that I am interested in. I designated the class, and have it set to print the 'Stats' class so that I can change the values later on with user input. The console is printing the stats like I want, but with an added 'None' after the numbers. Can someone explain why this is happening, and how I should fix it? If you're just going to post code without an explanation please don't... I'm trying to learn, after all :) Code: class Stats: def __init__(self, strength=0, dexterity=0, wisdom=0, intelligence=0): self.strength = strength self.dexterity = dexterity self.wisdom = wisdom self.intelligence = intelligence def getStats(self): print(self.strength, self.dexterity, self.wisdom, self.intelligence) def statinput(self, value1, value2, value3, value4): self.value1 = strength_input self.value2 = dexterity_input self.value3 = wisdom_input self.value4 = intelligence_input if strength_input: self.strength = self.strength + value1 if dexterity_input: self.dexterity = self.dexterity + value2 if wisdom_input: self.wisdom = self.wisdom + value3 if intelligence_input: self.intelligence = self.intelligence + value4 player = Stats(1, 1, 1, 1) print(player.getStats()) strength_input = int(input('Enter a value for strength.')) dexterity_input = int(input('Enter a value for dexterity.')) wisdom_input = int(input('Enter a value for wisdom.')) intelligence_input = int(input('Enter a value for intelligence.')) player_stat_choice = player.statinput(strength_input, dexterity_input, wisdom_input, intelligence_input,) print(player.getStats())All of the above code functions, but is printing the following to console: 1 1 1 1 None Enter a value for strength.2 Enter a value for dexterity.2 Enter a value for wisdom.2 Enter a value for intelligence.2 3 3 3 3 NoneI'm not sure if the 'None' portion is going to affect anything later on down the road after I've expanded this bit, so I'd like to handle it now :) Thanks ahead for your time. RE: Printing class results returns 'none' - Windspar - Aug-16-2019 It because you print the method return. There nothing to return so None is return. Remove the print from print(player.getStats())and None won't print anymore. RE: Printing class results returns 'none' - Lawr3y - Aug-16-2019 (Aug-16-2019, 12:53 AM)Windspar Wrote: It because you print the method return. There nothing to return so None is return. Ah something so trivial easily resolved. My apologies for wasting anyone's time, and thanks! RE: Printing class results returns 'none' - ThomasL - Aug-16-2019 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) RE: Printing class results returns 'none' - Windspar - Aug-16-2019 I do mine the lazy way. def __repr__(self): return "Stats" + str(vars(self))Why is it a bad idea ? Method name is bad. To me is should be output_stats not get_stats. RE: Printing class results returns 'none' - Lawr3y - Aug-16-2019 (Aug-16-2019, 01:57 PM)ThomasL Wrote: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. Woah, thanks for taking the time to explain everything thoroughly, it goes a long way with helping me understand this stuff. I'll have a peek at PEP 8 and see if I can improve the readability of my code for you guys, as I'm sure you'll be seeing me from time to time whenever I hit a wall and google doesn't solve my problem. I most definitely need to look into dunder-methods so I can clean up obsolete methods like the one you pointed out. Thanks again for the info! |