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

Topher Knoll
Topher Knoll
3,572 Points

Loop or if statement?

I'm not sure if I should be doing a list or if statement here. Neither work anyway. Can someone give me some direction? The challenge is pretty unclear.

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

    def __iter__(self):
        yield from self.pattern

    def __str__(self):
        output = []
        for blip in self:
            if blip == '.':
                output.append('dot')
            else:
                output.append('dash')
        return '-'.join(output)

    @classmethod
    def from_string(cls, string):
        output = []
        if string == 'dash':
            output.append('_')
        elif string == 'dot':
            output.append('.')
        return cls(output)



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

2 Answers

Steven Parker
Steven Parker
229,732 Points

You've got the right idea about using a loop to convert each part of the string one at a time. You may need to break the string up into pieces first, the "split" operation might be useful for that.

Steven Parker
Steven Parker
229,732 Points

Yes, the test you have is good for part of the string but not the whole thing. But if you break it up and use a loop to go through each bit one at a time, then that test would go inside the loop.

Topher Knoll
Topher Knoll
3,572 Points

I still don't get it. So do I loop through and THEN made an if statement? These code challenge would be better if they gave more specific reasons for why it's wrong and if they showed what was being passed in.