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

What am I doing wrong here?

What exactly am I doing wrong here? Can someone explain please.

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

    def __str__(self):
        pattern = [".", "_"]

        for pat in pattern:
            if pat == ".":
                self.pattern.append("dot")
            elif pat == "_":
                self.pattern.append("dash")
            return "-".join(pattern)


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

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

You have a couple issues here, but you're overall pretty close.

First of all, your return statement is indented to be inside your for loop. Thus, no matter how many elements are in self.pattern, your loop will always stop and your function will return after analyzing just the first element. You can mitigate this by unindenting that return statement a level, so it's even with your for loop

Second, you're creating a new list called pattern inside your __str__ function with some elements in it, looping over that, and appending your results to self.pattern, which is an entirely different list. Instead, you should be creating a new, empty list (which you can name how you please), looping over self.pattern, and appending the result to the new list you created at the beginning of the __str__ function. At the end of the function after the loop, you can return that array, and you should be good to go

After you fix those 2 issues, your code passes the challenge on my machine. Great job!

I don't understand what you mean by appending the results to self.pattern. Do I do that on another line? Would I do self.append?

Michael Hulet
Michael Hulet
47,913 Points

Nope, I'm referring to the 2 lines where you have self.pattern.append. Instead, you should be appending to a new list that you created at the beginning of the function

Something like this

def __str__(self):
    patterns = []

    for pat in patterns:
        if pat == ".":
            self.patterns.append("dot")
        elif pat == "_":
            self.patterns.append("dash")

    return "-".join(self.pattern)
Michael Hulet
Michael Hulet
47,913 Points

Not quite. In your code patterns is an entirely different list from self.patterns. I'd name it something different so that's more clear tbh. You want to iterate over self.patterns and only append to your local patterns (which I recommend you rename). You'll also have to join your local patterns when you return, too, and not self.patterns. You can think of self.patterns as being your input and patterns being your output

It looks like you have your return indented properly now, however. You're almost there!