Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


[Reverse Evens] My code works, but cannot past test

The Question: 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].

so here's my code, I do it in workspace first to test out, it works, but I couldn't get past test, what's the problem? thanks in advance

list = [1,2,3,4,5,6,7,8,9,10]

def reverse_evens(x):
    if x[-1]%2==0:
        return x[-1:0:-2]
        return x[-2:0:-2]

2 Answers

You need to return every item with an even INDEX. You are currently checking if the actual number is even. For example, regardless of what the numbers are, if the list is [3, 5, 7, 9, 2, 9], the index values are [0, 1, 2, 3, 4, 5], those are what you need to be concerned about, not if the actual number is even.

oh you're right, i looked past the index, mixed up thanks alot

Adam Paciorek
Adam Paciorek
3,181 Points


I have a problem here as well.

My function is :

def reverse_evens(list):
    reversed_list = list[::-1]
    return reversed_list[::2]

and gets a correct output for [1, 2, 3, 4, 5] but does not pass the test...

try this check again with a list that contains an even number of values, you will see you don't get back the correct result. you need to alter the code to work for both even and odd sets of data.