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 trialmellowmushroom
9,250 Pointsfor create function reverse_evens, how come this is not acceptable: def reverse_evens(iter): return iter[::-2]
for create function reverse_evens, how come this is not acceptable: def reverse_evens(iter): return iter[::-2]
def first_4(iterable):
return iterable[:4]
def first_and_last_4(iterable):
first_4 = iterable[:4]
last_number = iterable[-1:]
last_3 = iterable[-4:-1]
last_4 = last_3 + last_number
added = first_4 + last_4
return added
def odds(iterable):
return iterable[1::2]
def reverse_evens(iterable):
return iterable[::-2]
3 Answers
Steven Parker
231,269 PointsYou don't know if you're getting evens or odds.
That would certainly give you every other item in reverse, but whether those items are from the even indexes or the odd indexes would depend on how long the list is.
You'll need to either compute where to start based on the length, or you'll need to get the evens first and then reverse them in a separate operation (perhaps by using two slices).
mellowmushroom
9,250 Pointsoh ok thanks!
mellowmushroom
9,250 PointsHey Dean,
this is how I ended up implementing the function:
def reverse_evens(itera):
count = 0
for i in itera:
count = count + 1
if count % 2 == 0:
print(itera[-2::-2])
return itera[-2::-2]
else:
print(itera[::-2])
return itera[::-2]
Steven Parker
231,269 PointsYou don't need to print anything for this challenge. But otherwise, good job.
Dean Meyer
1,378 PointsDean Meyer
1,378 PointsHi Steven
I kinda see where you are getting at, however, I just keep getting it wrong. Do you mind sharing your code with over here (the even odd slicing question). Would be great!
Thanks!