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 trialMathew Yangang
4,433 Pointsreturning the right pattern
I thought they wanted me to return ("-" "."), but not sure why I keep getting "didn't get the right pattern" error
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, pattern):
fix_me =Letter("-" ".")
return fix_me
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
3 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThe __str__
method uses the list of “.” and “_” stored in self.pattern
to create a string of “dot” and “dash”.
The challenge is looking for the opposite. A from_string
class method that takes any string containing “dot”s and “dash”s, splits it up and converts it into a list of “.” and “_”, then uses that is to create an instance of Letter
.
A for
loop works well for this.
Post back if you need more help. Good luck!!!
Chris Freeman
Treehouse Moderator 68,457 PointsYou are close. They are looking for an underscore ("_")
Mathew Yangang
4,433 PointsHello Chris , I tried the underscore, but I still get the error "didn't get the right pattern"
Mathew Yangang
4,433 PointsI just did
Mathew Yangang
4,433 PointsMathew Yangang
4,433 PointsHi Chris , I tried to convert it into the requested value like thus:
I followed the steps you mentioned above but , i'm still getting "try again" errors. Also i would like to know how to use a for loop for this challenge, although i think i am kind of close with the above solution.
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsCan you post your latest version?
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsYour current solution does not use the value passed into the parameter
pattern
. You'll need afor
loop that splitspattern
changing any "dot"s and "dash"es to "." and "_".