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 trialPéter Juhász
4,768 Pointsreverse_evens gave an error message
Why the reverse_evens is not correct. I tried in IDLE with the example and it worked for me. Note: The example gives backs the odd items in reverse order. I tried with return iter4[-2::-2] which give back the evens in reverse order but it didn't work.
def first_4(iter1):
return iter1[:4]
def first_and_last_4(iter2):
return iter2[:4] + iter2[-4:]
def odds(iter3):
return iter3[1::2]
def reverse_evens(iter4):
return iter4[-1::-2]
3 Answers
Steven Parker
231,248 PointsThe code shown will work when the argument has an odd number of elements. Otherwise, it will return "reverse odds" instead.
To always return reverse odds, there's two basic strategies that are equally effective: you can compute the starting position based on the size, or you can extract the even indexes first and then reverse them in a separate step.
Péter Juhász
4,768 PointsThanks. I returned the following slice in my code and it was accepted. in_string[::2][::-1] It returns odd elements in the reverse order so the name of the function is still wrong!!
Steven Parker
231,248 PointsThat code should always return the items with even-numbered indexes (reversed), which is what the challenge asked for. Good job!
Péter Juhász
4,768 PointsThanks. I understood now.