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 trialSimon Amz
4,606 Pointsresult on morse.py
Hi, In the code attached, here is my str function, in ordre to replace "." by "dot" and "_" by "dash".
I test this code in my text editor with the following code and it works perfectly, but it raises an error on Treehouse text editor, I don't know why.
Here is the lines for the main except for the Letter class:
class S(Letter): def init(self): pattern = ['.', '', 'hello', 5.2, 'dog', '.', '', '.'] super().init(pattern)
simon = S() simon.str(simon.pattern)
and it gave me
"dot-dash-hello-5.2-dog-dot-dash-dot"
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
def __str__(self, pattern):
final_pattern = []
for letter in pattern:
if letter == ".":
final_pattern.append("dot")
elif letter == "_":
final_pattern.append("dash")
else:
final_pattern.append(str(letter))
final_pattern = "-".join(final_pattern)
return(final_pattern)
`
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
4 Answers
Stuart Wright
41,120 PointsHere are a couple of tips to help you arrive at the correct solution:
- The str() method only needs one argument, 'self'.
- You can then loop over 'self.pattern' instead of 'pattern'.
Simon Amz
4,606 PointsI tried and I got the same thing, everything works on my computer. Besides I don't have details about the error, I just got "BUMMER: Try again!"
Here is my code again:
#!usr/bin/python3
morse
class Letter: def init(self, pattern): self.pattern = pattern
def __str__(self):
final_pattern = []
for letter in self.pattern:
if letter == ".":
final_pattern.append("dot")
elif letter == "_":
final_pattern.append("dash")
else:
final_pattern.append(str(letter))
final_pattern = "-".join(final_pattern)
print(final_pattern)
class S(Letter): def init(self): pattern = ['.', 'dog', '', '*', '', 'test', '.', '.'] super().init(pattern)
simon = S() simon.str()
Stuart Wright
41,120 PointsClose! You just need to return rather than print final_pattern.
Simon Amz
4,606 PointsGreat it works, I was sure to replace the print by return and it didn't work as well. Never mind, thanks very much!