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 trial

Python

Heidi Ulrich
Heidi Ulrich
4,624 Points

Trouble 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?

morse.py
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)
Venkat SOMALARAJU
Venkat SOMALARAJU
2,448 Points

I tried the following code, still no luck.

<p>
class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern

    def __iter__(self):
        for item in self.pattern:
        yield item

    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=None):
        pattern1 = []
        new_list = string.split("-")
        for item in new_list:
            if item == "dash":
                pattern1.append("_")
            if item == "dot":
                pattern1.append(".")

        return cls(pattern1)

class S(Letter):
    def __init__(self):
        pattern = ['.', '.', '.']
        super().__init__(pattern)

</p>

Could share your thoughts?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

There 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 method pattern.append('_')

  • fixing this lead to: TypeError: 'str' object does not support item deletion is caused by the statements del 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
Heidi Ulrich
4,624 Points

Ah, 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.