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 trialTerry Felder
1,738 Pointsi ran the example and a string, through my function and it work why won't it pass?
def reverse_evens(iterable):
return iterable[::-2]
def first_4(iterable):
return iterable[:4]
def first_and_last_4(iterable):
first4 = iterable[:4]
last4 = iterable[-4:]
together = first4 + last4
return together
def odds(iterable):
return iterable[1::2]
def reverse_evens(iterable):
return iterable[::-2]
2 Answers
Philip Schultz
11,437 PointsHey, The reverse evens isn't working because you are assuming the length of the list is odd, what if the length of the list is even? It would start at an odd index then skip by 2, right. So, you can do this two ways. First, you can test the length to determine if its odd or even, then return a statement accordingly. Like so,
def reverse_evens(iterable):
if len(iterable) % 2 == 0:
return iterable[-2::-2]
else:
return iterable[::-2]
return reverse_evens
Second, you can make a list of all evens going forward, then reverse. Like so,
def reverse_evens(iterable):
even = iterable[::2]
reverse_evens = even[::-1]
return reverse_evens
Terry Felder
1,738 Points!!!! its so awesome and frustrating realinzing the answer is so simple lol thank you