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

Eldin Guzin
Eldin Guzin
6,010 Points

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
231,275 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.

Eldin Guzin
Eldin Guzin
6,010 Points

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
231,275 Points

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

Eldin Guzin
Eldin Guzin
6,010 Points

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
231,275 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).

Eldin Guzin
Eldin Guzin
6,010 Points

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
231,275 Points

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