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 trialItsa Snake
3,852 PointsMorse code task might have an error?
I keep getting this error message, which seems to be the output it wants?
Bummer: Didn't get the right string output. Got: dot-dot-dot for S()
here is the original question:
Challenge Task 1 of 1 Let's use str to turn Python code into Morse code! OK, not really, but we can turn class instances into a representation of their Morse code counterparts.
I want you to add a str method to the Letter class that loops through the pattern attribute of an instance and returns "dot" for every "." (period) and "dash" for every "_" (underscore). Join them with a hyphen.
I've included an S class as an example (I'll generate the others when I test your code) and it's str output should be "dot-dot-dot".
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
def __str__(self):
morse_text = []
for item in self.pattern:
if item == ".":
morse_text.append("dot")
elif item == "_":
morse_text.append("dash")
morse_text = "-".join(morse_text)
return morse_text
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsHi there,
You've pretty much got everything correct, except I believe you misread part of the instructions. The challenge instructs you to add a _str_
method to the Letter class, but you added it to the S class.
Simply move your code to the proper class and the challenge will pass.
Nice work! :)
Itsa Snake
3,852 PointsItsa Snake
3,852 PointsThanks a lot. Silly of me