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

Atıl Kurtulmuş
Atıl Kurtulmuş
4,945 Points

Why isn't this code being accepted?

I tried running on pycharm with input [1, 2, 3, 4, 5] and it returned [5, 3, 1]. works perfectly fine. Why wont the workshop accept it?

slices.py
def first_4(x):
    return x[:4]

def first_and_last_4(a):
    return a[:4] + a[-4:]

def odds(xyz):
    return xyz[1::2]

def reverse_evens(abc):
    if len(abc) % 2 == 0:
        return abc[-1::-2]
    else:
        return abc[::-2]

2 Answers

I'm not sure why your code isn't working... But it is possible to do it the longer way:

def reverse_evens(list_):
    result = []
    for item in list_:
        if list_.index(item) % 2 == 0:
            result.append(item)
    return result[::-1]

Maybe try your code with a list with an even length!

Atıl Kurtulmuş
Atıl Kurtulmuş
4,945 Points

NVM! solved it by changing the code to this:

def reverse_evens(abc):
    if len(abc) % 2 == 0:
        return abc[-2::-2]
    else:
        return abc[::-2]