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

后端 未结 3 1712
情话喂你
情话喂你 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:09

    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

提交回复
热议问题