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

[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]
    else:
        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

Hello,

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.