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 trialMatthew Barton
1,322 PointsTask 4 reverse_evens doesn't work for me. Why?
I tried my code in a REPL and it works fine for the example input but when I check my work it says it returned the wrong value. What is the problem with reverse_evens? I don't know where to begin on how to fix it.
def first_4(list):
return list[:4]
def first_and_last_4(list):
return list[:4] + list[-4:]
def odds(list):
return list[1::2]
def reverse_evens(list):
return list[::-2]
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsYou're on the right track. However, you are not taking into account a list with an even number of items as opposed to a list with an odd number of items.
Hit: try reversing the whole list, then grab every other item.
Post back if you need more help. Good luck!!
Matthew Barton
1,322 PointsMatthew Barton
1,322 PointsI changed it to this and it works for both even and odd length lists. In my REPL it still works and I tried a bunch of test cases but the problem still says I don't return the correct values. What is my issue?
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsMatthew Barton, good question!! This had me stumped at first as your code appears to be written correctly.
The issue is the
reverse()
method changes the list in place. Since thelist
argument is mutable, the first timereverse_evens()
is run, the argument itself is reversed, If run with the same argument again, the result changes:One fix would be to make a local copy of the
list
argument before reversing.Matthew Barton
1,322 PointsMatthew Barton
1,322 PointsI finally got it to work! Thank you for the help.