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

Andrei Oprescu
Andrei Oprescu
9,547 Points

I don't get this

Hi!

I am currently on this question that confuses me. My code currently is:

class Letter: pattern1 = [] def init(self, pattern=None): self.pattern = str()

def __str__(self):
    pattern = pattern.split(', ')
    for sign in pattern:
        if sign == '.':
            pattern1 + 'dot'
        elif sign == '-':
            pattern1 + 'dash'
    pattern1 = '-'.join(pattern1)
    return pattern1

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

And the question is:

Let's use str to turn Python code into Morse code! OK, not really, but we can turn class instances into a representation of their Morse code counterparts.

I want you to add a str method to the Letter class that loops through the pattern attribute of an instance and returns "dot" for every "." (period) and "dash" for every "_" (underscore). Join them with a hyphen.

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".

I can probably understand that I am doing something wrong as I am not getting thin one correct.

Could you please help me?

Thanks!

morse.py
class Letter:
    pattern1 = []
    def __init__(self, pattern=None):
        self.pattern = __str__()

    def __str__(self):
        pattern = pattern.split(', ')
        for sign in pattern:
            if sign == '.':
                pattern1 + 'dot'
            elif sign == '-':
                pattern1 + 'dash'
        pattern1 = '-'.join(pattern1)
        return pattern1

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

3 Answers

Dan Garrison
Dan Garrison
22,457 Points

You are close, but there are a few issues.

  1. You don't need the empty list at the beginning of the class.
  2. In the init, you should set self.pattern = pattern.
  3. At the beginning of your str method, you should initialize an empty list.
  4. Call it something like str_pattern and set the value to an empty list [].
  5. In your for loop, you need to look for sign in "self.pattern". The self.pattern is referencing the pattern attribute for the current instance of your Letter object.
  6. In your if/else statement, you should append either "dot" or "dash" to your empty list.
  7. At the end, you should return your str_pattern list
Andrei Oprescu
Andrei Oprescu
9,547 Points

I understand, but I still get an error to this:

class Letter: def init(self, pattern=None): self.pattern = pattern

def __str__(self):
    str_pattern = []
    for sign in self.pattern:
        if sign == '.':
            str_pattern.append('dot')
        elif sign == '_':
            str_pattern.append('dash')
    '-'.join(str_pattern)
    return str_pattern

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

Dan Garrison
Dan Garrison
22,457 Points

You would need to return the '-'.join(str_pattern)

Sorry should have been more clear on that.

Andrei Oprescu
Andrei Oprescu
9,547 Points

Thanks a lot! it actually worked!