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


I just can't wrap my head around these last 3 challenges

I only kinda understand the decorator, but but the examples given were not as clear as others in the tract. I believe I would need to make an iterative loop to convert the string but I'm drawing a blank on how.

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 == '.':
        return '-'.join(output)

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

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

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

Let's break down the problem from the description.

  • Create a class method... -- need to creating a method decorated with @classmethod
  • ... in Letter named from_string... -- method is called from_string and goes in the class Letter
  • ... that takes a string like "dash-dot"... -- the method has one parameter, you can expect it to be a string
  • ...and creates an instance... -- classmethods do not have an instance to operate on, so you can create one (see below)
  • ...with the correct pattern (['_', '.']). -- You're correct, in that a loop will be needed to parse the string (using split on a hyphen seems appropriate) and convert each "dash" to an '_' underscore and each "dot" to a '.' period. Use this list of parsed characters called, say 'pattern, you can create a new instance usinginst = cls(pattern)`
  • remember to return the new instance.

Post back if you need more help. Good luck!