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

returning the right pattern

I thought they wanted me to return ("-" "."), but not sure why I keep getting "didn't get the right pattern" error

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, pattern):
        fix_me =Letter("-" ".")
        return fix_me


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

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The __str__ method uses the list of “.” and “_” stored in self.pattern to create a string of “dot” and “dash”.

The challenge is looking for the opposite. A from_string class method that takes any string containing “dot”s and “dash”s, splits it up and converts it into a list of “.” and “_”, then uses that is to create an instance of Letter.

A for loop works well for this.

Post back if you need more help. Good luck!!!

Hi Chris , I tried to convert it into the requested value like thus:

@classmethod   # declaring class method
def from_string(cls, pattern):  # a from_string class that takes any string containing "dot" and "dash"
      fix_me = Letter([".", "_"])   # Splits it up and convert it into a list , then create the instance
      return fix_me   # return the value requested.

I followed the steps you mentioned above but , i'm still getting "try again" errors. Also i would like to know how to use a for loop for this challenge, although i think i am kind of close with the above solution.

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Your current solution does not use the value passed into the parameter pattern. You'll need a for loop that splits pattern changing any "dot"s and "dash"es to "." and "_".

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You are close. They are looking for an underscore ("_")

Hello Chris , I tried the underscore, but I still get the error "didn't get the right pattern"

I just did