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 bummer! My function return the expected result but the challenge refuse the answer.

def reverse_evens(iterable):
    return iterable[-1::-2]

Any idea?

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

def first_and_last_4(iterable):
    return iterable[0:4] + iterable[len(iterable) - 4:]

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

def reverse_evens(iterable):
    return iterable[-1::-2]

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Well, you think you returned the expected result but didn't do complete testing.

If you run:

def reverse_evens(iterable):
    return iterable[-1::-2]

print(reverse_evens([1,2,3,4,5]))
print(reverse_evens([1,2,3,4,5,6]))

You'll see the output for case 1 is correct while case 2 returns the odd/incorrect values:

[5, 3, 1]
[6, 4, 2]
Schaffer Robichaux
Schaffer Robichaux
21,729 Points

Hey Samuel, The keyword that Kenneth uses in the video is 'even' indexes. This provides the clue that you need to determine the inclusiveness of your slice based off of whether or not the starting index is even. To further Dave's point above, I often build very simple "test harnesses" to evaluate my scripts that produce seemingly correct answers- they've always helped me with debugging and are well worth the time. A sample for your problem is below:

test1 = list(range(10))
test2 = 'potato'
test3 = list('potato')
test4 = [1, 2, 3, 4, 5]

def test_harness_1(func, test1, test2, test3):
    func(test1)
    func(test2)
    func(test3)

test_harness_1(reverse_evens, test1, test2, test3)

You can add print statements in your code as needed to evaluate the results.