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 trialSuresh N
743 PointsCode working in Workspaces, but throwing error in Challenge window.Need help.
Code working in Workspaces, but throwing error in Challenge window.Need help.
def first_4(items):
return items[0:4]
def first_and_last_4(items):
slice1=items[0:4]
slice2=items[-1:-5:-1]
slice1.extend(slice2)
return slice1
3 Answers
Dave StSomeWhere
19,870 PointsWhile you think your code is working, it is not producing the expected results.
The challenge is asking for you to return the first 4 and last 4 of a list - it doesn't say to reverse the the order of the last 4 (which your code produces) - I agree that is the last 4 and they really didn't specify an order but you're a step ahead and the challenge only accepts the last 4 in normal order.
Please adjust your slice2 to get the last 4 in the original order and you should be good to go...
You seem to have a good handle on slices so shouldn't be a problem, just ask if more info needed.
Suresh N
743 PointsThanks Dave. I changed the code as per your comments. Still am unable to get through in challenge window. Its working well in Workspaces. def first_4(items): return items[0:4]
def first_and_last_4(items): slice1=items[0:4] slice2=items[-1:-5:-1] slice2.sort() slice1.extend(slice2) return slice1
Dave StSomeWhere
19,870 PointsWe can't use sort()
since there doesn't have to be any specific order of the elements.
Now, you can reverse slice2 again instead of the sort but for this challenge it is much simpler.
Remember that you can control the start, stop and step of the slice. Hint you only need 1 of the three.
Check out this SO Answer for a pretty good explanation.
Does that help
Suresh N
743 PointsOh..got it. I am through now. Thank you very much for the hint.Appreciate it.