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 trialDaniel Murray
3,294 PointsCould someone please explain why my function isn't correct?
Hi,
On task 4 of Pyhton Collections challenge 'Slice Functions', the last function 'reverse_evens'. The code challenge is returning a 'Bummer: didn't get the right values from 'reverse_evens' error. But when I paste the code into the shell it returns the correct result.
The challenge is: "Make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse.
For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1]."
https://teamtreehouse.com/library/python-collections-2/slices/slice-functions
Thanks,
Daniel
def first_4(iterable):
return(iterable[:4])
def first_and_last_4(iterable):
return(iterable[:4] + iterable[-4:])
def odds(iterable):
return(iterable[1::2])
def reverse_evens(iterable):
return(iterable[::-2])
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Daniel Murray ! It looks like you're doing terrific and yes, your last function does work, but only sometimes. Notably, it works when the length of the iterable is an odd number. For instance, here, you send in [1, 2, 3, 4, 5]
and get back [5, 3, 1]
, but if you send in [1, 2, 3, 4, 5, 6]
you get back [6, 4, 2]
. I would expect to still get back [5, 3, 1]
as those are the numbers at the even indexes from the beginning.
Suggestion: try getting every other one first and then reversing the result.
Hope this helps!
Daniel Murray
3,294 PointsDaniel Murray
3,294 PointsOk got it, thank you Jennifer! Really helped. 😎