Why does this script print an extraneous 'none' in the output

后端 未结 3 1719
情话喂你
情话喂你 2020-12-12 03:43

I\'ve written a simple script to help me better understand using classes. It generates a random character for a game. I defined the object and then call a function on that o

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-12 04:02

    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.

提交回复
热议问题