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 trialHeidi Ulrich
4,624 PointsTrouble creating a class method
I have watched the video, but it's not really dawning yet. Thus I have, I expect, taken a difficult and unnecessary swing at this. Could somebody help me a little on what I should really do?
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
def __iter__(self):
yield from self.pattern
def __str__(self):
output = []
for blip in self:
if blip == '.':
output.append('dot')
else:
output.append('dash')
return '-'.join(output)
@classmethod
def from_string(cls, string):
pattern = []
if string:
while len(string) > 0:
if string.endswith('dot'):
pattern.append['.']
del string[:-4]
continue
if string.endswith('dash'):
pattern.append['_']
del string[:5]
continue
return list(reversed(pattern))
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsThere are two errors I see:
The challenge error of
TypeError: 'builtin_function_or_method' object is not subscriptabe
is caused by using square brackets instead of parens when calling the methodpattern.append('_')
fixing this lead to:
TypeError: 'str' object does not support item deletion
is caused by the statementsdel string[:5]
. Strings are immutable, that is, they cannot be changed, or sliced, in this case.
Post back after fixing these if you need more help. Good luck!!
Heidi Ulrich
4,624 PointsAh, I fixed this today, in a breeze - and I took out all of the code that creates that weird list with dels and such. I just suddenly had it. But now I go back to the editor, it only shows the pre-state of when I hadn't typed it in... I'm sorry I don't have it offline saved somewhere.
I passed the challenge, but err... I cannot reproduce the code to help others. Sorry, failure of Treehouse here.
Venkat SOMALARAJU
2,448 PointsVenkat SOMALARAJU
2,448 PointsI tried the following code, still no luck.
Could share your thoughts?