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

Andrei Oprescu
Andrei Oprescu
9,547 Points

I don't know what I did wrong

So i have this question on one challenge:

Let's practice using @classmethod!

Create a class method in Letter named from_string that takes a string like "dash-dot" and creates an instance with the correct pattern (['_', '.']).

and my code is at the bottom.

Can someone tell me what I missed?

Thanks!

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, blip_list):
        blips = []
        blip_list.split('-')
        for blip in blip_list:
            if blip == 'dot':
                blips.append('.')
            elif blip == 'dash':
                blips.append('-')
        return blips


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

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,094 Points

Andrei Oprescu, you close to a solution. Three items need fixing.

  • the results of blip_list.split('-') isn't assign to a variable so it is lost. You reassign the results back to blip_list of needed.
  • your dash string should be an underscore (_) instead of a hyphen (-)
  • the challenge asks for and instance be returned, not the list. Instead use the list you created to create a new instance. Hint you can use the cls() available.

Post back if you need more help. Good luck!!!

Nik Omel
seal-mask
.a{fill-rule:evenodd;}techdegree
Nik Omel
Python Web Development Techdegree Student 7,496 Points
    @classmethod
    def from_string(cls, words):
    output = []
    words = words.split("-")
    for word in words:
        if word == "dot":
            output.append('.')
        elif word == "dash":
            output.append('_')
    return cls(output)```

What am I doing wrong?
Chris Freeman
Chris Freeman
Treehouse Moderator 68,094 Points

Please do not simply post cut-and-paste solutions. Instead try to address what issues in the original post. Thanks.