Ask coding questions

← Back to all posts
Python Class error
tankerguy1917 (178)

I am using a class Player and I am getting an error saying that __init__() takes 3 positional arguments but 4 were given when I only put in 3 arguments. Did I do something wrong when making the class, because I usually don't get this error whenever I make classes.

Comments
hotnewtop
[deleted]

You forgot (by all the classes) a 'self' in the beginning so, for the class 'Player' you write.
def init(self, hp, mp, name):

tankerguy1917 (178)

@Tercoder Ah, how could I have forgotten that. Thanks.

Coder100 (18199)

You forgot the self argument, here is the correct code

class Entity:
	def __init__(self, hp, mp, name):
		self.hp = hp
		self.mp = mp
		self.name = name

class Player(Entity):
	def __init__(self, hp, mp, name):
		self.hp = hp
		self.mp = mp
		self.name = name

the self argument is used to refer to the class itself.