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

Magic method str, I'm getting expected output string on my machine. Output is "class 'str'"

Not sure why the output is not matching.

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

    def __str__(self):
        dotlist = []
        dashlist = []
        for xpattern in self.pattern:
            if "." == xpattern:
                dotlist.append("dot")
            else:
                dashlist.append("dash")

        return "-".join(dotlist) + "-" + "-".join(dashlist)


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

xpattern = S()
xpattern.__str__()

2 Answers

Rather than a dot list followed by a dash list there should be a single list containing a possible mix of dots and dashes. So for example a pattern of ['.', '_', '.', '_'] would return:

dot-dash-dot-dash

instead of

dot-dot-dash-dash

Thank you for the response. Instruction is not clear "I've included an S class as an example (I'll generate the others when I test your code) and it's str output should be "dot-dot-dot"."

Update the code as follow: class Letter: def init(self, pattern=None): self.pattern = pattern

def __str__(self):
    mixlist = []
    for xpattern in self.pattern:
        if "." in xpattern:
            mixlist.append("dot")
        else:
            mixlist.append("dash")

    dotstr = "-".join(mixlist)
    return dotstr

class S(Letter): def init(self): pattern = ['.', '-', '.', "", "."] super().init_(pattern)

def hello(self):
    return "Hello String"

pattern = S() p = pattern.str() print(p) This is returning "dot-dash-dot-dash-dot"

I'm getting the same error: Bummer: Didn't get the right string output. Got: dot-dash-dot-dash-dot for S()

The S class is an example and shouldn't be edited. Other tests will be done as indicated by the instructions: (I'll generate the others when I test your code). The only code you need to provide is the __str__ method in class Letter. If you add just your updated code for this method you should pass the challenge.