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 trialtink3r
1,787 PointsWondering if anyone can tell me why this code isn't passing?
def first_and_last_4(itr): first_4 = itr[:4] last_4 = itr[:-5:-1] first_and_last_4 = first_4 + last_4 first_and_last_4.sort() return first_and_last_4
def first_4(itr):
return itr[:4]
def first_and_last_4(itr):
first_4 = itr[:4]
last_4 = itr[:-5:-1]
first_and_last_4 = first_4 + last_4
first_and_last_4.sort()
return first_and_last_4
5 Answers
tink3r
1,787 PointsThat was the problem! Thank you Alex.
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi tink3r,
Your problem is sorting the output list. Suppose your input list is:
['d', 'a', 'b', 'e', 'q', 'x', 'z', 'y', 'w']
You want to return
['d', 'a' ,'b', 'e', 'x', 'z', 'y', 'w']
but because you are sorting your list you are returning
['a', 'b', 'd', 'e', 'w', 'x', 'y', 'z']
The solution is just to remove first_and_last_4.sort()
.
Hope that helps,
Alex
tink3r
1,787 PointsI though that as well so I removed the sort and it still didn't pass.
Alex Koumparos
Python Development Techdegree Student 36,887 PointsI copied and pasted your code into the challenge and deleted the line with sort() and it did pass. Did you copy-paste or retype the code? Maybe you made a typo when re-entering? What error message are you receiving exactly?
tink3r
1,787 PointsI just tried it again and the error I get is...
Bummer! Didn't get the right values from first_and_last_4
.
def first_4(itr): return itr[:4]
def first_and_last_4(itr): first_4 = itr[:4] last_4 = itr[:-5:-1] first_and_last_4 = first_4 + last_4 return first_and_last_4
Alex Koumparos
Python Development Techdegree Student 36,887 PointsOk, not sure why it seemed to pass when I tried it the first time. The other problem with your code is that last_4 is in the wrong order. When you slice from the end backwards you get the last four items in reverse order. To get the last four items in the correct order you slice as follows:
my_slice = my_list[-4:]