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

Task 4 of the Code Challenge is... picky?

Hi,

For the last task, 4 of 4, the auto-grader won't accept this solution:

def reverse_evens(iter_item):
    return iter_item[-1::-2]

However, running this locally works:

>>> a = list(range(1,6))                                                                                  
>>> a                                                                                                     
[1, 2, 3, 4, 5]                                                                                           
>>> a[-1::-2]                                                                                             
[5, 3, 1]

Any of you ran into this as well? Thanks!

1 Answer

Your code sort of works, but it only works for lists that have an odd length.

If the length of the list is even, you should doiter_item[0::-2]. However, as I said, that snippet only works for lists length even.

So, you would probably have to use an if/else statement to test if the length of the list is even or odd :smile:


Here's a hint:

To test if a number is even, you can do this:

Pretend I'm in the Shell

>>> a = 10
>>> a % 2 == 0  # This tests "is a even?", and it is so Python returns True
True
>>> b = a - 1  # In this case, b will be 9 (which is a - 1)
>>> b % 2 == 0
False

If you have questions on the % operator, respond below with the text #modoperator inside and I'll explain. :)


I hope this helps :grin:

Happy coding! :tada:

:dizzy: ~Alex :dizzy: