Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,410 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!