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!

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


Morse code challenge - can't solve it!

Afternoon all,

I've been staring at this for a while now and have browsed other solutions within the Community pages but can't see what I've done wrong here.

I think continuing to stare at the code is unlikely to produce a different result so I could do with some help, please!



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

    def ___str___(self):
        output = []
        for c in self.pattern:
            if c == '.':
            elif c == '_':
        return "-".join(output)

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

2 Answers

Steven Parker
Steven Parker
228,025 Points

Your code is fine. :+1:

You just have the wrong name for your method. You have "___str___" (with 6 underscores), but the name should be "__str__" (with 4 underscores).

Thanks Steven,

That's very odd, though. In the IDE, there's two underscores each side so there must be some weird copy/paste issue that I created.

Thanks again - that'll give me something more interesting to stare at today! :smile:



Thomas Beaudry
Thomas Beaudry
29,084 Points

Did it work OK Steve Hunter, I am currently on this also? Thank you :)

Yep - all worked fine. :+1:

Hey Steve.. did you find a solution to this challenge? I tried writing the same code you have above but with double underscores but my code still isn't passing

Steven Parker
Steven Parker
228,025 Points

When I checked his code the first time, I copied the above and pasted it directly into the challenge. And then after only changing the name, it passed.