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

My function gives the correct result but it's not accepting

Why isn't it accepting even when my program returns the required result

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

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

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

def reverse_evens(lis):
    return lis[-1::-2]
Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Did you only test the 1 example provided in the challenge - this list - [1, 2, 3, 4, 5]?

Do you get a correct result if you test a different list, for example [1, 2, 3, 4, 5, 6]?

2 Answers

Adriano Junior
Adriano Junior
5,065 Points

Parvati, your code seems fine, but as we all know the Treehouse Interpreter can sometimes make you feel crazy. I've just finished this challenge, and this is what I used.

Step 1

iterable =[] def first_4(iterable): return iterable[:4]

Step 2

def first_and_last_4(iterable): del iterable[4:-4] return iterable

Step 3

other_iterable = [] def odds(other_iterable): return other_iterbale[1::2]

Step 4

last_iterable = [] def reverse_evens(last_iterable): normal_order = last_iterable[::2] in_reverse = normal_order[::-1] return in_reverse

Steven Parker
Steven Parker
230,995 Points

This code would produce the correct result only sometimes, based on the length of the array. To make it work in all cases, there's two basic strategies you can use:

  • compute the starting point based on the length of the array
  • select the even indexes first, and then reverse them in a separate operation

Properly implemented, either method will pass the challenge.