Why Does This Script Print An Extraneous 'none' In The Output
Solution 1:
print player.stats()
Is the culprit. player.stats() == None
You want just:
player.stats()
You'd do better to name your function player.printStats()
.
Another option would be to make it return a string:
def stats(self):
return '\n'.join([
self.name
"Strength : %d" % self.strength,
"Dexterity : %d" % self.dexterity,
"Hit Points: %d" % self.hit_points,
"Aura : %d" % self.aura,
"Weapon : %s" % self.weapon,
"Spell : %s" % self.spell,
"Item : %s" % self.item,
"Element : %s" % self.element,
"-" * 20
])
And then print player.stats()
would behave as expected
Solution 2:
The stats()
method does not return anything. A function that doesn't return anything evaluates to None
. Which is what you print.
So, don't print the return value of the function. Just call it. Now, you should rename the function as printStats()
to make it clear what it does. And then just call it like this:
def printStats(self):
....
player = Player(name)
player.printStats()
Naming is a really important part of programming. A slightly poor choice of name often leads to confusion like this.
Solution 3:
You print the return value of player.stats()
:
print player.stats()
but the .stats()
method does not have a return statement. The default return value of any function or method in python is None
, if no return statement has been given:
>>> def foo():
... pass
...
>>> print foo()
None
Either remove the print
before the method call, or have stats()
return a string to print instead of doing all the printing in the method.
Post a Comment for "Why Does This Script Print An Extraneous 'none' In The Output"