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


hi im getting error

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

and code

class enemmy :     life = 3     self = ""     def attack(self):         print ("ouch!!!!")         self.life -= 1      def checklife(self):         if self.life <= 0 :             print ("dead")         else:             print (self.life)  enemy=enemmy enemy.attack() 

i checked , looked places says forgot self in def attack or need make obj put class in im useing python 3.4 py charm got code tutorial , dont know mistake

you're not instantiating enemy class. creating new reference class itself. when try , call method, calling without instance, supposed go self parameter of attack().

change

enemy = enemy 

to

enemy = enemy() 

also (as pointed out in kevin in comments) enemy class should have init method initialise fields. e.g.

class enemy:     def __init__(self):         self.life = 3     ... 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -