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 trialKars Jansens
5,348 PointsI'm not sure why this isn't working.. {python}
Question: 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".
It works in workspave and I can't find the problem.
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
def __str__(self, pattern):
lista = []
for item in pattern:
if item == ".":
lista.append("dot")
if item == "_":
lista.append("dash")
string = "-".join(lista)
return string
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsYou are very close! The __str__
method of the class should operate on the instance attribute pattern
and not on a pattern
passed into the method. Remove pattern
from the method parameter list and loop over items in self.pattern
.
Post back if you need more help. Good luck!