Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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
216,164 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
216,164 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.