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

Adam Tyler
Adam Tyler
14,865 Points

Am I using __iter__ properly? I can't get this challenge to pass

I am unsure what I am doing wrong her, I put it into work spaces and it tells me I have inconsistent use of tabs and spaces in indentations

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

    def __str__(self):
        morseStr = ''
        for i in self.pattern:
            if i == '.':
                morseStr += '-dot'
            elif i == '_':
                morseStr += '-dash'
        morseStr = morseStr[1:]

        return morseStr

    def __iter__(self):
        yield from self.pattern


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

1 Answer

Steven Parker
Steven Parker
231,261 Points

I know that Python will work with either spaces or tabs, but it won't allow you to mix them in the same file. But I don't see that here.

On the other hand, it looks like the "__str__" method has been changed from what was provided in the initial challenge code. For this challenge, you only need to add the "__iter__" method. Don't change any of the provided code!

Your "__iter__" method looks correct and should pass the challenge with no other changes made.

Adam Tyler
Adam Tyler
14,865 Points

Thank you, I have since passed this challenge but the reason I was having an issue was because some of the lines in the code provided had too many spaces (the indentation was 5 spaces and this was leaving several red lines all over the code) so I edited that, but in the end I passed because I restarted and didn't edit anything. Odd, but thank anyway!

Steven Parker
Steven Parker
231,261 Points

I didn't notice a space difference, but the content of the "__str__" method above is very different from the one the challenge provides. This one builds a string with concatenation, the challenge code builds a list and then joins it. Both are valid solutions, however.