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

add a __str__ method to the Letter class that loops through the pattern attribute of an instance and returns "dot" for e

I'm stuck need help.

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


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

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

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Karen,

Well, you may just kick yourself after I point out the error.

Your syntax is spot on, but you're not appending to the string dictionary you defined. You've mistakenly used the temporary variable in the loop as the the name of what you're trying to append to. So, instead of pattern.append("dot") and pattern.append("dash") you do string.append("dot") ... etc and your code will pass with flying colors. :smiley:

NIce work!! :) :dizzy:

Oh, and watch. There seems to be an extra space in the indent for def __str__(self):.