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 trialRyan Gostic
20,790 PointsNo method named from_string
I clearly have a method named from_string in my Letter class.
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, strCode):
array = strCode.split("-")
selfPattern = 0
for v in array:
if v == "dash":
selfPattern.append("-")
elif v == "dot":
selfPattern.append(".")
return cls(selfPattern)
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
1 Answer
lemelleio
10,874 PointsHey again,
Your code is almost correct, just two things and it passes. As far as the errors go in these code challenges it seems the validation is written is such a way to give a very general error at times when they're are multiple things wrong.
anyway,
@classmethod
def from_string(cls, strcode):
array = strcode.split("-")
selfpattern = [] #changed from 0 to a list
for v in array:
if v.lower() == "dash":
selfpattern.append("_") #changed from - to an _
elif v.lower() == "dot":
selfpattern.append(".")
else:
pass
return cls(selfpattern)
Ryan Gostic
20,790 PointsRyan Gostic
20,790 PointsThanks!