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 trialScott Larson
2,813 PointsI can't figure out why the tests for slices.py step 4 are failing.
Here is the code: def reverse_evens(item): return(item[-1::-2])
I have verified it works outside of the code checker. I just want to pass this step.
def first_4(iterable):
return iterable[:4]
def first_and_last_4(i2):
return(i2[:4]+i2[-4:])
def odds(i3):
return i3[1::2]
def reverse_evens(i4):
val = i4[:]
return val[-1::-2]
3 Answers
Brian McKinney
10,119 PointsSorry, looked at the question and had my order wrong. This should work:
def reverse_evens(i4):
val = i4[:]
return val[::2][::-1]
someguy1234
3,450 PointsI just try and it didn't work
def reverse_evens(x4):
return x4[::-1][::2]
Then I switch to do a [::2] first then reverse with [::-1]. Can you please explain why? Or it was the instructions said so?
okay, I've tested it and understand why now
if x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] x[::-1][::2] = [9, 7, 5, 3, 1]
but x[::2][::-1] = [8, 6, 4, 2, 0]
we need 'evens' then reverse. sooo I get it now.
Brian McKinney
10,119 PointsI believe you need to reverse it first and then take the evens.
return val[::-1][::2]
james south
Front End Web Development Techdegree Graduate 33,271 Points[::-2] doesn't slice out the even indices, it slices out every other index starting at the last one. if the length of the list is odd (last index thus even number) this works fine, but if the list length is even (last index odd number), you are merely slicing out the odd indices. one way to solve would be to test for list length evenness and if even, slice off the final element, then use [::-2], if odd, straight to [::-2]
Scott Larson
2,813 PointsScott Larson
2,813 PointsThe code displayed should be 'val = i4[-1::-2]', I tried to just return it, I tried enclosing it in parens, etc. I can't get this to work in the code window. The test case provided works outside the test box.