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

I can't get past this question, I'm getting a generic "Try again!" message.

Here's my code

def reverse_evens(list): list.sort() if list[-1] % 2 == 0: return list[-1::-2] else: return list[-2::-2]

slices.py
def first_4(list):
    return list[0:4] 
def first_and_last_4(list):
    list = list[0:4] + list[len(list)-4:len(list)]
    return list

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

def reverse_evens(list):
    list(list)
    list.sort()
    if list[-1] % 2 == 0:
        return list[-1::-2]
    else:
        return list[-2::-2]

1 Answer

Ernestas Petruoka
Ernestas Petruoka
1,856 Points

Well first of all I would change given argument in your code, because list are function to make a list and I don't know for sure if it's good to use that as argument. Then if you make a list from a string you need to use variable in which you do that for example l = list(argument). Also you don't need sort list. Then you check not the last index value in list, but lenght of the list (if it's even or odd lenght long). So you need use if len(list) % 2 == 0:, not if list[-1] % 2 == 0. And the last problem is that you need change places in return's. If len(list) % 2 == 0: you need return list[-2::-2] else return return list[-1::-2].

The code should look like this:

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

Hope that will help you ! BTW Sory if I made some grammar mistakes, still learning english :)

Thanks! This solved my issue. I see how you were able to handle this problem.