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 trialMikaela Land
Python Web Development Techdegree Student 2,324 PointsCouldn't find reverse evens
So on the reverse_ evens I'm not sure why treehouse says it can't find it. Any ideas. Oh and I'm not even sure what I did with the for i part but I was recommended it. I asked but got no response. Any explanations?
def first_4 (fourth):
return fourth [:4]
def first_and_last_4 (first_last):
first = first_last [:4]
last = first_last [-4:]
return first + last
def odds(iter):
result_iter = []
for i in range(len(iter)):
if i % 2 != 0:
result_iter.append(iter[i])
return result_iter
def reverse_evens (it):
it_reverse: []
for i in range(len(it)):
if (i) % 2 == 0:
it_reverse.append [::-2]
return it_reverse
I also tried: def reverse_evens (it): for i in range(len(it)): if (i) % 2 == 0: it_reverse = it [::-2] return it_reverse
1 Answer
Steven Parker
231,275 PointsI don't think you can use the slice operator on a method name. And you can do this without a loop.
To always return reverse evens using only slice(s), there's two basic strategies:
- compute the starting position based on the list size
- extract the even index values first, then reverse them
Either one will pass the challenge when implemented correctly.
Mikaela Land
Python Web Development Techdegree Student 2,324 PointsMikaela Land
Python Web Development Techdegree Student 2,324 PointsStill not understanding I'm sorry. I've tried getting the starting postion but it seems I've forgotten how. and I did a for loop for the index values and tried to reverse them. No luck. Keep getting a TypeError
Steven Parker
231,275 PointsSteven Parker
231,275 PointsThe second method is the easiest. Use one slice to get the even-indexed items, and then another slice to put them in reverse order. No loop is needed and it can be done on one line.
Mikaela Land
Python Web Development Techdegree Student 2,324 PointsMikaela Land
Python Web Development Techdegree Student 2,324 PointsOMG! I got it. It's been two days of messing with it! Thanks so much Steven!