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

Igor Skoldin
Igor Skoldin
6,779 Points

My method is not passing

My method seems to create an instance with the correct pattern but does not pass the challenge. Did I make a mistake?

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 = []

        for item in string.split('-'):
            if item == 'dot':
                pattern.append('.')
            else:
                pattern.append('-')

        return cls(pattern)

1 Answer

Christian Mangeng
Christian Mangeng
15,970 Points

Hi Igor,

it's a small mistake: dash is an underscore '_', not a hyphen '-':

pattern.append('_')
Igor Skoldin
Igor Skoldin
6,779 Points

This was it, indeed, thank you. I used hyphen because hyphen was used above, in the str method, which is a part of initial task code. But this hyphen was used as a separator, not as a part of the pattern.