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

what is my mistake?

from a task in object-oriented python,construction zones, i keep getting the error message that it is incorrect output.

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, strings):
        morse = strings.split('_')
        patterns = (['_', '.'])
        for word in morse:
            if patterns == 'dot':
                word.append('.')
            elif patterns == 'dash':
                word.append('_')
        return cls(patterns)        

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

1 Answer

Steven Parker
Steven Parker
231,275 Points

You clearly have the right idea, but:

  • you probably meant to split on hyphens ("-") instead of underscores ("_")
  • the "patterns" should start out as an empty list
  • you'll want to compare against "word" and append to "patterns" (instead of vice-versa)

Fix those issues and you should pass!

Thanks i managed to figure it out. There was also an issue of indentation it would seem but it all worked out.

Steven Parker
Steven Parker
231,275 Points

Khumo Setlhabi — the indentation on your code looks good to me as-is. But now that I take a closer look, the spacing on that "class S" code looks funny. But they supplied it like that, and I didn't have to change it to make it work.

Anyway, glad I could help. You can mark a question solved by choosing a "best answer".
And happy coding!