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 trialTravis Vance
2,989 PointsReverse Evens Challange
This code works as expected in console, with example list and others (i.e. returns only even numbers in reverse order. However, it fails in the slices.py test. Can someone point out what I'm missing? Thanks!
def first_4(items):
items_list = list(items)
thing = items_list[0:4]
return thing
def first_and_last_4(items):
items_list = list(items)
thing1 = items_list[0:4]
thing2 = items_list[-4:]
thing3 = thing1 + thing2
return thing3
def odds(items):
items_list = list(items)
thing = items_list[1::2]
return thing
def reverse_evens(item):
item_list = list(item)
reverse_list = item_list[::-1]
evens = reverse_list[1::2]
return evens
2 Answers
Steven Parker
231,275 PointsThis code will work in half of the possible cases, depending on the length of the list. You probably tested it with "lucky data".
But you can make it work in all cases by extracting the even indexed items first, and then reversing them.
Travis Vance
2,989 PointsThanks, Steven!
Steven Parker
231,275 PointsTravis Vance — Glad to help. You can mark a question solved by choosing a "best answer".
Happy coding!