hi im getting this error
TypeError: attack() missing 1 required positional argument: \'self\'
and this is my code
class En
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.
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
...