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 trialTruong Le
2,583 Pointsmorse.py - what's wrong with this Code?
I could not figure out the reason why this was not right. When I ran the code, the output seems to be right: dot-dot-dot. Could anyone please help. Thanks a lot.
class Letter: def init(self, pattern=None): self.pattern = pattern
class S(Letter): morse_array = []
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
def __str__(self):
i = 0
for item in self.pattern:
if item == '.':
self.morse_array.append("dot")
i += 1
if i < len(self.pattern):
self.morse_array.append('-')
elif item == '_':
self.morse_array.append("dash")
i += 1
if i < len(self.pattern):
self.morse_array.append('-')
return ''.join((self.morse_array))
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
class S(Letter):
morse_array = []
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
def __str__(self):
i = 0
for item in self.pattern:
if item == '.':
self.morse_array.append("dot")
i += 1
if i < len(self.pattern):
self.morse_array.append('-')
elif item == '_':
self.morse_array.append("dash")
i += 1
if i < len(self.pattern):
self.morse_array.append('-')
return ''.join((self.morse_array))
2 Answers
Steven Parker
231,248 PointsThe instructions said, "I want you to add a __str__
method to the Letter class", but it looks like it was added to the "S" class instead.
Also, "self.morse_array" is being referenced before being created. It doesn't need to be an instance variable, so you could optionally leave the "self." off.
And while conditionally appending hyphens will work, you wouldn't need to if you use a hyphen instead of a empty string when you "join" the elements.
Truong Le
2,583 PointsWow, thanks a lot for your prompt help! Everything cleared and checked!