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

Oprea Mihai
Oprea Mihai
5,592 Points

Letter

I can't pass from this step. can you help me ?

morse.py
class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern
    def __str__(self,pattern=None):
        pattern = tuple()
        for letter in pattern:
            if letter == '.':
                letter_1 = 'dot'
            else:
                letter_1 = 'dash'
            pattern += (letter_1,)
        pattern = '-'.join(self.pattern)
        return self.pattern





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

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Opera,

You've got the right idea, and you're most of the way there.

The main problem is that you aren't using the instance's pattern property. To access that, you need to use self.pattern. This is the property that holds the .s and -s, and is the one you're going to want to loop through. In your code, you're looping through an empty tuple!

The part you did correctly is that you do also need to create an empty iterable (can be a tuple as you've done, or a list), but you probably want to call it something other than pattern, just to avoid confusion. Maybe something like word_pattern.

Once you have both of these, you'll want to loop through self.pattern, because that's where the .s and -s are, and then add to the word_pattern the way you're doing already.

Let me know if this makes sense and if you can figure it out with these pointers!

Cheers :beers:

-Greg

Oprea Mihai
Oprea Mihai
5,592 Points

I've tried but still doesn't work:

          <p>class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern

    def __str__(self,pattern=None):
        self.pattern = tuple()
        self.word_pattern=tuple()
        for letter in self.pattern:
            if letter == '.':
                letter_1 = 'dot'
            else:
                letter_1 = 'dash'
            self.word_pattern += (letter_1,)
        self.word_pattern = '-'.join(self.word_pattern)
        return self.word_pattern</p>
          ```
Greg Kaleka
Greg Kaleka
39,021 Points

Opera,

You're still not using self.pattern. Now you're setting self.pattern to an empty tuple. self.pattern is already set when the instance is created, so in the __str__ method, you can just go ahead and use it directly. The str method also doesn't need to take a pattern as an argument. In fact, in python str can't take any additional arguments besides self.

So basically, you're just going to create the str method that just takes self, and loop through self.pattern. Again, this line:

self.pattern = tuple()

deletes the contents of self.pattern, and sets it to an empty tuple. That's not what you want.