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 trialRohela Raouf
2,520 PointsTypeError: __init__() missing 1 required positional argument: 'name' keeps appearing, not sure why
When I try to run the following code:
import random
class Character:
def __init__(self, name, **kwargs):
self.name = name
for key, value in kwargs.items():
setattr(self, key, value)
class Thief (Character):
sneaky= True
def pickpocket(self):
return self.sneaky and bool (random.randint(0,1))
The following error message appears in the console: TypeError: init() missing 1 required positional argument: 'name'
I am not sure why this is happening, would someone be able to provide some clarity to why I get this message?
3 Answers
Scott Bailey
13,190 Pointsimport random
class Character:
def __init__(self, name, **kwargs):
self.name = name
for key, value in kwargs.items():
setattr(self, key, value)
I'm on my phone currently so can't check the code fully, but I think the issue may have been your indentation.
the def and for statement only needed to be indented 4 spaces self.name needs to be indented 4 spaces after the : as does the setattr()
Evrything you want to happen after a : needs to be on a new line and indented another 4 spaces.
Give it a go and let us know if it works!
Rohela Raouf
2,520 PointsThanks, I will look at that.
Pratham Mishra
14,191 PointsHey Rohela, I think the problem is with your indentation. You have to indent 4 spaced for self.name = name and the setattr statement. I hope this fixes your problem