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

Lukas Straumann
seal-mask
.a{fill-rule:evenodd;}techdegree
Lukas Straumann
Python Web Development Techdegree Student 9,588 Points

How do I avoid the creation of a dash-dot string when the class is created? Thanks for any help on this.

My code actually creates the ['_','.'] from ('dash','dot') but when I return this as a class (cls) in the classmethod, then it gets converted back to a text, as the provided class does this with a provided argument. Any help on whats missing or wrong is greatly appreciated.

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, astring):
    words = astring.split('-')
    output = []
    for blip in words:
        if blip == 'dot':
            output.append('.')
        if blip == 'dash':
            output.append('_')
    print(output)
    return cls(output)

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

letter_obj = Letter.from_string('dash-dot') print('letter_obj: ',letter_obj)

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, apattern):
        #print('here',apattern)
        words = apattern.split('-')
        output = []
        for blip in words:
            if blip == 'dot':
                output.append('.')
            if blip == 'dash':
                output.append('_')
        return cls(output)


class S(Letter):
    def __init__(self):
         pattern = ['.', '.', '.']
         super().__init__(pattern)
Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

hi Lukas. You can delete the question, under your post it should be 3 dots which has edit or delete option. Happy coding

1 Answer