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

Mario Sanchez Carrion
Mario Sanchez Carrion
17,541 Points

My code is not passing. Could you take a look?

I believe my loop is OK. If I do this manually in the command line it works, however my code is not passing the challenge.

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

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



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

1 Answer

Marcin Lubke
Marcin Lubke
13,253 Points

Actually your code is pretty ok, but you have some unnecessary __str__ method declaration. You don't need __str__ declaration inside S class, it will be inherited from the Letter class, also you need to change the declaration of the __str__ method inside Letter class. You don't have to pass a new argument in here, your pattern is already know as self.patter variable, so you have to change it to:

    def __str__(self):

And here, inside your for loop:

       for item in self.pattern:
            if "." in self.pattern:
                morse.append('dot')
            elif "_" in self.pattern:
                morse.append('dash')
        return "-".join(morse)

you compare dot and dash to self.pattern. You should compare it to "item". Also in my opinion, it would be better to use "==", but both ways should work.