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 trialTopher Knoll
3,572 PointsLoop or if statement?
I'm not sure if I should be doing a list or if statement here. Neither work anyway. Can someone give me some direction? The challenge is pretty unclear.
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
def __iter__(self):
yield from self.pattern
def __str__(self):
output = []
for blip in self:
if blip == '.':
output.append('dot')
else:
output.append('dash')
return '-'.join(output)
@classmethod
def from_string(cls, string):
output = []
if string == 'dash':
output.append('_')
elif string == 'dot':
output.append('.')
return cls(output)
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
2 Answers
Steven Parker
231,269 PointsYou've got the right idea about using a loop to convert each part of the string one at a time. You may need to break the string up into pieces first, the "split" operation might be useful for that.
Topher Knoll
3,572 PointsI still don't get it. So do I loop through and THEN made an if statement? These code challenge would be better if they gave more specific reasons for why it's wrong and if they showed what was being passed in.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsYes, the test you have is good for part of the string but not the whole thing. But if you break it up and use a loop to go through each bit one at a time, then that test would go inside the loop.