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 trialVishay Bhatia
10,185 PointsShouldn't this technically work?
Am I not fulfilling the question? I feel the answer needs to be a certain way in order to pass these assignments.
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
def __str__(self, pattern):
str1 = ''
for i in self.pattern:
if i == '.':
str1+= 'dot'
else:
str1+= 'dash'
return '-'.join(str1)
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
1 Answer
Jon Mirow
9,864 PointsHi there!
Ah you're close it's just a couple of little errors. Firstly you don't need any arguments for your str method, because you are accessing the pattern property of the class directly. It will also cause an error because you're changing the interface of the str method by adding required arguments to it. it's safe to delete that argument.
Secondly, it's just a little thing, you've used a string to store the characters. What that means is that you'll end up with "dotdotdot" and calling the join method will result in "d-o-t-d-o-t-d-o-t". Just change the datatype you're using to a list, and append each time so you end up with a list similar to ["dot", "dot", "dot"], so that the join method creats the string "dot-dot-dot". My example:
def __str__(self):
output = []
for i in self.pattern:
if i == '.':
output.append('dot')
elif i == "_":
output.append('dash')
return'-'.join(output)
Christopher Carucci
1,971 PointsChristopher Carucci
1,971 PointsThe answer above is good and explains a lot, nice job.