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 trialAryan Monga
4,061 PointsA quiz question!
Great work! OK, this second one should be pretty similar to the first. Make a new function named first_and_last_4. It'll accept a single iterable but, this time, it'll return the first four and last four items as a single value.
def first_4(thing):
return thing[0:4]
def first_and_last_4(a):
return a[0:4]
return a[-1:-5:-1]
1 Answer
Steven Parker
231,269 PointsYou can only use return one time.
When the function encounteres a return statement, it ends; so no further code will be performed in the function.
But you can combine values using concatenation ("+
") in a single return statement.
Aryan Monga
4,061 PointsAryan Monga
4,061 PointsDidn't know that! Thanks!
Aryan Monga
4,061 PointsAryan Monga
4,061 PointsThere is an error that says "Didn't get the right values." What should I write for the slices? Please help!
Steven Parker
231,269 PointsSteven Parker
231,269 PointsYou already know how to do the first 4. Just figure out how to do the last 4 and then concatenate those two.
Aryan Monga
4,061 PointsAryan Monga
4,061 PointsI tried but I didn't get the answer. Could you help with the code? Thnx;)
Steven Parker
231,269 PointsSteven Parker
231,269 PointsFor the "last n" items you can use "-n" as the slice start value, and you won't need a stop or step value. For example, the last 7 items of a list name "month": "
month[-7:]
".If you still have trouble, be sure to show your new code.