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

Hi everyone, kindly help me figure out my error.

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

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

    def __str__(self):
        new_pattern = ""

        for item in self.pattern:
            if item == "." :
                new_pattern = new_pattern + "dot"

            elif item == "_" :
                new_pattern = new_pattern + "dash"

        return new_pattern

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

2 Answers

I would change new_pattern to be a list and then append each item to the list and then return the list using a join. ''.join(new_pattern)

In [1]: pattern = ['.', '-', '.']

In [2]: ''.join(pattern)
Out[2]: '.-.'

However, I am not sure I understood what error you were experiencing so please take this comment / answer with a grain of salt

thanks Josh, I followed your approach and it worked