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

Andrew Wilcox
Andrew Wilcox
4,374 Points

morse.py @classmethod code challenge! - I don't know what I'm doing wrong - getting the wrong pattern

I seem to be sending the wrong pattern with my code.

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, str):
        str = "dash-dot" 
        list_pattern = str.split("-")
        for word in str:
            if word == 'dash':
                list_pattern.append('-')
            elif word == 'dot':
                list_pattern.append('.')

        return cls(list_pattern)





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

1 Answer

AJ Salmon
AJ Salmon
5,675 Points

Hi Andrew!

Couple issues here, but they're easy to fix. Firstly, you don't need that 'str' variable, and it's messing up the challenge. The challenge will call the method with an argument like that, but not exactly that. It could be "dash-dot-dot-dash-dot" or just "dot". Secondly, you're splitting the string on the hyphen, which is smart, but you're appending the same list that you create when you split it, so you're adding periods and dashes to a list that also has "dash" and "dot" in it. To fix that, just create a new, separate, empty list at the start, then use that list for appending and your return. Lastly, despite the wording, the "dash"'s should be converted to underscores ( _ ) not hyphens ( - ). Hope this helps!

AJ

Andrew Wilcox
Andrew Wilcox
4,374 Points

Hey AJ, thanks for the prompt response. This is what I did after your suggesting, but still send wrong pattern :(

@classmethod def from_string(cls, str): new_output = [] for string in new_output: if string == 'dash': new_output.append('_') elif string == 'dot': new_output.append('.')

    return cls(new_output)
AJ Salmon
AJ Salmon
5,675 Points

Ah, I should clarify that you still need to split() the string on the hyphens, it should just be a separate step.

@classmethod
def from_string(cls, str):
    new_output = []
#then split the string
    for string in str.split('-'):
    if string.lower() == 'dash':
        new_output.append('_')

So you're breaking the string up into a list of "dot"'s and "dash"'s, and then looping through them, each time inserting a period or underscore if the string it's on is "dot" or "dash".

AJ Salmon
AJ Salmon
5,675 Points

Aha, glad you got it :) My pleasure!