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 trialprateekmahesh
5,454 PointsCan some one tell me how to solve task 4 in 'SLICE FUNCTIONS' task
I am not sure about what should be returned in the last function
def first_4(my_iterable):
return my_iterable[0:4]
def first_and_last_4(my_iterable2):
join = my_iterable2[0:4] + my_iterable2[len(my_iterable2)-4:]
return join
def odds(my_iterable3):
return my_iterable3[1::2]
def reverse_evens(my_iterable4):
return my_iterable4[2::]
2 Answers
Vidhya Sagar
1,568 PointsThe output for you're code is the even indexes will be returned in the actual order. But the challenge is asking for the reverse of it .So save the actual order in a variable and return the inverse of the variable by using [::-1] on it . Try it on you're own. Ive added my code for reference.
SPOILER ALERT
def reverse_evens(my_iterable4):
a=my_iterable4[::2]
return a[::-1]
jonathan chadeyras
15,015 PointsHi,
I post that here because I think it's somehow related.
I tried that and it works in my terminal but not on the website:
def reverse_evens(item):
return item[::-2]
any one has an idea why?
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsActually, your code sort of works, but it won't work if the list's length is an odd amount.
Vidhya Sagar
1,568 PointsVidhya Sagar
1,568 PointsWhy are u saying it does not work for lists with odd length ? I did try it and it works fine.
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsNevermind. I thought I read somewhere else that this won't work with an odd amount of elements.