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

Challenge task 4 of the python slices challenge won't let me pass even though the code is correct.

I tried the code in Visiual Studio to make sure it works as intended and it does. For some reason I get an error for the reverse evens function.

Either return list[::-2] or return list[-1::-2] should work, but neither work in the challenge :/

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

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

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

def reverse_evens(list):
    return list[::-2]

1 Answer

Louise St. Germain
Louise St. Germain
19,424 Points

Hi!

You're almost there - the issue is that when you start slicing from the end with the negative numbers, you haven't found out whether the list has an even or odd number of items. The code you have does work if the list provided has an odd number of a items, but everything is off by one if the list has an even number of items. You will need to add a bit more code to reverse_evens to account for the fact that the list could have either an odd or even number of items.

I hope this helps!