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.

Python

construction zone help

I do not know what did I did wrong it says that i didn't get the right pattern, can someone give me some hints of what am I doing wrong ? Thanks for your help !

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, strings):
        regular_pattern = []
        strings = strings.split("-")
        for dash_dot in strings:
            if dash_dot == ".":
                regular_pattern.append("dot")
            elif dash_dot == "-":
                regular_pattern.append("dash")
        return cls(regular_pattern)



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

3 Answers

Steven Parker
Steven Parker
216,012 Points

This method is intended to look for "dot" and "dash" tokens in a string and convert them into a pattern made of period (".") and underscore ("_") elements.

But right now, its doing the opposite — looking for pattern elements and converting them to the words.

It still says that it doesn't get the right pattern...

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, strings):
    regular_pattern = []
    strings = strings.split("-")
    for dash_dot in strings:
        if dash_dot == "dot":
            regular_pattern.append(".")
        elif dash_dot == "dash":
            regular_pattern.append("-")
    return cls(regular_pattern)

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

Steven Parker
Steven Parker
216,012 Points

Almost! But instead of a hyphen, a "dash" should be converted into an underscore.

This is becoming frustrating, I am really sorry for bothering you Steven but I dont get why is it holding me back on these simple mistakes, btw you are awesome,stay safe in these difficult time wherever you may be !!!

@classmethod def from_string(cls, strings): dash_dot_pattern = [] strings = strings.split("-") if strings == "dot": dash_dot_pattern.append(".") elif strings == "dash": dash_dot_pattern.append("_") return cls(dash_dot_pattern)

Steven Parker
Steven Parker
216,012 Points

Without Markdown formatting, code is hard to read and some issues (like indentation errors) cannot be seen at all.

But it looks like this code is missing the loop needed to step through the code elements (and was there before).

Oh sorry, it's the same code as the above one but I changed the hyphen into a underscore, still says it gives the wrong result....

Steven Parker
Steven Parker
216,012 Points

The previous code works with the underscore. But if you still have trouble, repost the code again with formatting.