Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialQueenie Peng
8,052 PointsTypeError: __init__() takes 4 positional arguments but 5 were given
Here's my code, when I delete one argument it works, but with four, it gave me this mistake.
class Monster:
def __init__(self, hit_point, weapon, color, sound):
self.hit_point = hit_point
self.weapon = weapon
self.color = color
self.sound = sound
>>> from monster import Monster
>>> a = Monster(5, 'sword', 'blue', 'roar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes 4 positional arguments but 5 were given
1 Answer
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Queenie
I have just run your code without any errors. Check you indentation in the init method. This is my code, the str method just prints out the attributes of the class in a more readable format.
class Monster:
def __init__(self, hit_point, weapon, color, sound):
self.hit_point = hit_point
self.weapon = weapon
self.color = color
self.sound = sound
def __str__(self):
return "{} {} {} {}".format(self.hit_point,self.weapon,self.color,self.sound)
from monster import Monster
a = Monster(5, 'sword', 'blue', 'roar')
print(a)
hope this helps !
Queenie Peng
8,052 PointsQueenie Peng
8,052 PointsStrange, I tried again just now and it worked! Thanks a lot!