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

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,558 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,558 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