TypeError: attack() missing 1 required positional argument: 'self'

前端 未结 2 1926
陌清茗
陌清茗 2020-12-07 04:52

hi im getting this error

TypeError: attack() missing 1 required positional argument: \'self\'

and this is my code

class En         


        
相关标签:
2条回答
  • 2020-12-07 05:01

    You need to create and use an instance of the class, not the class itself:

    enemy = Enemmy()
    

    That instance is then accessible as self. If you don't have an instance, then it's missing and that's what the error message tells you.

    0 讨论(0)
  • 2020-12-07 05:13

    You're not instantiating your Enemy class. You are creating a new reference to the class itself. Then when you try and call a method, you are calling it without an instance, which is supposed to go into the self parameter of attack().

    Change

    enemy = Enemy
    

    to

    enemy = Enemy()
    

    Also (as pointed out in by Kevin in the comments) your Enemy class should probably have an init method to initialise its fields. E.g.

    class Enemy:
        def __init__(self):
            self.life = 3
        ...
    
    0 讨论(0)
提交回复
热议问题