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 trialErnestas Petruoka
1,856 PointsNot really understand why it is not working!
Can someone explain me why it is not working? And what I should do to make it work? I want you to add a str method to the Letter class that loops through the pattern attribute of an instance and returns "dot" for every "." (period) and "dash" for every "_" (underscore). Join them with a hyphen.
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
def __str__(self, pattern):
d = []
for patt in self.pattern:
if str(patt) == '.':
d.append('dot')
elif str(patt) == '_':
d.append('dash')
return join,('-', d)
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
print(super().__init__(pattern))
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Ernestas,
You have a couple of problems with your __str__
method.
First, you have the wrong method signature. The challenge is expecting you to have a method that can be called like this:
s = S()
str(s)
(This will produce the output: 'dot-dot-dot'
)
Since str(s)
is equivalent to calling s.__str__()
we can see that the __str__()
method must have self
as its only parameter. Because you have a second parameter, pattern
, the method would have to be called like this:
s.__str__(s.pattern)
This breaks the str()
method which relies on the object's __str__()
method being called with no arguments.
Thus an attempt to do this:
s = S()
str(s)
will produce this error:
TypeError: __str__() missing 1 required positional argument: 'pattern'
Note also that even if the method signature for __str__
worked, you don't actually use the value passed in as pattern
. Inside your method you're just using self.pattern
which refers to a totally different variable.
Your second error is that you are not using the string method join()
correctly. Remember that the syntax for join() is: some_string.join(some_list)
where some_string
is the string you want between each item and some_list
is the list you are joining.
Hope that clears everything up for you.
Cheers,
Alex
Ernestas Petruoka
1,856 PointsErnestas Petruoka
1,856 PointsThank you very much! It is working now.