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 think there is something wrong with this question.

Okay so I am on the last challenge and I am told to create this reverse_evens() function. The challenge tells me that this input [ 1, 2, 3, 4, 5 ] should have this output [5, 3, 1 ]. I created that function and I even tried it in workspace and I received the same output the challenge wanted. But the challenge keeps telling me that I am not receiving the correct values. I just wanted to ask you guys am I looking over something or is this challenge playing with me?

slices.py
def first_4(word):
    return(word[:4])
first_4("hello")

def first_and_last_4(word):
    return(first_4(word) + word[-4:])
first_and_last_4("hello")

def odds(word):
    return(word[1::2])
odds("hello")

def reverse_evens(word):
    return(word[-1::-2])
reverse_evens("hello")

1 Answer

Steven Parker
Steven Parker
231,007 Points

Your code will work half of the time — when the list has an odd number of items in it. But when the list has an even number of items, it will return "reverse odds" instead.

There are two ways to make it work in all cases. One would be to compute the starting value based on the length of the list, and the other would be to extract the even indexed values first and then reverse them.

Also note that the challenge only asks you to define the functions, you don't need to call them.