Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Let's make our Letter class better for our Morse code challenge. Add an __iter__ method to the Letter class so the lette

Having some trouble with this challenge. I tried to implement the corrections that were recommended to Steve Hunter in his question about this challenge.. but it hasn't worked for me. Can anyone give me some advice on how to pass this challenge? thank you

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

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

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

5 Answers

Hey bro, You've got a triple underscore thing going on with your str function.

thanks Splotch.. I'm still stuck here though.. I get the error- Bummer: Letter isn't iterable

Hey,

Are you putting your code in the class Letter part or in the class S(letter)? When I first did this challenge it accidentally put my code at the bottom of S(letter) and I got the same error as you. Then I put it in the class Letter and it passed.

def __iter__(self):
        yield from self.pattern

Should pass if it is put in the class Letter but it will say Letter isn't iterable if its put in the class S(Letter). Hope that helps you.

Hi D Elis. The teacher gives example code where we build our own list from the ground up:

def __iter__(self):
    for item in self.slots:
        yield item

This snippet below is functionally identical:

def __iter__(self):
    yield from self.slots

Of course slots was specific to the example and not this challenge. I was able to pass it months ago, and just now. This OOP course was the toughest Python course I took... until the datetime course... and then until the regex course. But never mind those for now :)

Please let me know if I can help out further.

I tried putting those iter methods into my Letter class but I still get an error

I'm sorry to hear this :( Can you tell me what the error says?

Also, if you've tried running it in your console, does that tell you which line the error is on?

Hey Mark, My error says- Bummer: Letter isn't iterable

Jan Durcak
Jan Durcak
12,662 Points

Hey guys, What can you do with this piece of code, how can you iterate in you python console after is updated(python console)?

Bruce McMinn
Bruce McMinn
10,029 Points

Here's what I did. But I can't see difference the iter method made. I get the same output from print(s) either way.

deskerss-iMac:python deskers$ python3
Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from morsecode2 import S
>>> s = S()
>>> print(s)
dot-dot-dot
>>> 

Also, no difference if I use

    def __iter__(self):
        yield from self.pattern

or

    def __iter__(self):
        for blip in self.pattern:
            yield blip

I would be stoked to hear from someone on how to use the iter method. Thanks!

ps how do I escape dunders in markdown? they are making iter bold and not dundered...