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 trialPhilipp Räse
Courses Plus Student 10,298 PointsBummer! - "Didn't get the right output when iterating through a `Letter`"
Hello everybody,
I started the Code-Challenge "Iterable" and thought by myself 'how hard could it be?!'
But when I entered and submitted my code, I received the message, about a misbehaving output from my 'iter'-method.
I've tested it in my workspaces and it works as expected.
I hope you can hel me. Many thanks in advance!
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
def __str__(self):
output = []
for blip in self.pattern:
if blip == '.':
output.append('dot')
else:
output.append('dash')
return '-'.join(output)
def __iter__(self):
temp_list = str(self).split('-') # split the string and put the string-shards to a temporary list-object
yield from temp_list # loop and yield through the temporary list-object
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
1 Answer
andren
28,558 PointsYour code is quite clever, but you seem to have misunderstood the task slightly.
When the task states that you should not convert the pattern to dots and dashes in the __iter__
function they did not mean to suggest that you needed to achieve that functionality without replicating the __str__
code, like you have achieved. They actually meant that they didn't want the yielded output to be converted, period.
So the solution to this challenge is actually even simpler than the code you have written:
def __iter__(self):
yield from self.pattern # loop and yield through the `pattern` attribute
Philipp Räse
Courses Plus Student 10,298 PointsPhilipp Räse
Courses Plus Student 10,298 PointsThank you so much :)