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 trialAndrei Oprescu
9,547 PointsI 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!
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
Treehouse Moderator 68,453 PointsAndrei 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 toblip_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
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
Treehouse Moderator 68,453 PointsPlease do not simply post cut-and-paste solutions. Instead try to address what issues in the original post. Thanks.