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

Any idea why this code is not passing?

Any idea why this code is not passing?

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, pattern):
        result = []
        fix_me = Letter(['_','.'])
        for item in pattern:
            if item =='dash':
                result.append("_")
            else:
                result.append("dot")
        return ".".join(result)




class S(Letter):
    def __init__(self):
         pattern = ['.', '.', '.']
         super().__init__(pattern)
Steven Parker
Steven Parker
229,744 Points

Alex, the other question was about this same challenge, but the issues in the code are different.

My apologizes. I should have examined the code closer.

1 Answer

Steven Parker
Steven Parker
229,744 Points

You're close, but I see three issues:

  • the method should take a string, but this code is expecting a pattern list (try using "split")
  • the pattern should contain underscores and periods, but this code makes underscores and the word "dot"
  • the instructions say to return "an instance with the correct pattern", you may need "cls()" but not "join"

Also, "fix_me" is never used, so the line that creates it can be omitted.