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

Alexander Mitrofanov
Alexander Mitrofanov
5,029 Points

what is wrong with my solution?

I get: Bummer! Didn't get the right string output. Got: dot-dot-dot for S()

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

    def __str__(self):
        ret_str = ""
        for char in self.pattern:
            if char == ".":
                ret_str += "dot-"
            if char == '_':
                ret_str += "dash"
        if ret_str:
            ret_str = ret_str[:-1]
        return ret_str


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

1 Answer

Jeff Wilton
Jeff Wilton
16,646 Points

Though you might get the right output, I think it's looking for a different solution (maybe one that handles the dash with a hyphen as well).

In fact, I just verified that adding a hyphen to your "dash" string will complete the challenge.

ret_str += "dash-"

But in case you're interested, here is a different solution that I came up with.

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

    def __str__(self):
        s = "-";
        str_list = [] 
        for char in self.pattern:
            if char == ".":
                str_list.append("dot")
            if char == '_':
                str_list.append("dash")
        return s.join(str_list)


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