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 trial

Python Python Collections (2016, retired 2019) Slices Slice Functions

My code returns the right value in workspaces but doesn't get accepted by the quiz

def reverse_evens(iterable): return iterable[::-2]

print(reverse_evens([1, 2, 3, 4, 5])) returns [5, 3, 1]) in workspaces but doesn't get accepted by the quiz

slices.py
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

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Emanuele,

You're doing great so far.

But even though that one example will work, what happens if you have an iterable with an even number of items (eg. 1, 2, 3, 4, 5, 6)? Then your code will no longer return the items with an even index. It would return (6, 4, 2) from the iterable which have the indexes (5, 3, 1). This is why the task is failing in the challenge. The code checker is checking multiple variations of iterables against your code.

This task is a bit more complex than the other and will need a conditional check to decide which return needs to be used. HINT: You will need two different returns using an if / else clause.

Give it another try with this in mind. If you are still stuck, you can have a look at this post.

It's also good to remember that just because something works in your editor doesn't mean it will pass the challenges... Tasks are very specific and often check multiple possibilities.

Keep Coding! :) :dizzy:

Thank so much for the tip! After having that in mind the solution just came by itself, really appreciated!