Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Having problem with Morse code classes, please help.

Im not getting any errors when I run this in visual Studio code I dont understand what im missing

morse.py
class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern
    def __str__(self):
      str_list = []
      for sign in self.pattern:
        if self.pattern == '.':
          str_list.append('dot')
        elif self.pattern == '_':
          str_list.append('dash')
      final_list = '-'.join(str_list)
      return final_list

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

1 Answer

andren
andren
28,538 Points

In your for loop you compare the self.pattern array directly to "." and "_" instead of comparing the sign variable that is actually being produced by the for loop. Which seems like a typo to me. If you replace self.pattern with sign like this:

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

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


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

Then your code should work.

I ended fixing that last night lol yeah it was a typo. However even with the updated code it will not work

class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern
    def __str__(self):
      str_list = []
      for sign in self.pattern:
        if self.pattern == '.':
          str_list.append('dot')
        elif self.pattern == '_':
          str_list.append('dash')
      final_list = '-'.join(str_list)
      return final_list

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

That's strange, the code worked for me. I tested it on the challenge before I posted it.

I would recommend just restarting the challenge (using the "Restart" button found in the challenge workspace) and then you copy and paste in the code I embedded in my answer, that usually clears up any challenge bugs.

Also just as a side note the code you posted in your reply still has the typos in it.

Yeah sorry I pasted the wrong code it worked for me after refreshing. Thanks again