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 trialAbubakar Gataev
2,226 PointsSLICE FUNCTIONS HEELP
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. The first one is right, the second is wrong please help.
def first_4(favotings):
return favotings[0:4]
def first_and_last_4(ABUISBEEST):
return ABUISBEEST[0:4]
return ABUISBEEST[6:100]
1 Answer
Abubakar Gataev
2,226 PointsWhat do I have to do to reach the second line then?
Jigar Gor
Courses Plus Student 1,568 PointsIf your first statement returns, you will never be able to get to the second line. You must consolidate the two slices you want to return. You can either assume only strings will be passed and use the '+' operator, or check for what type the iterable passed is before using an appropriate operator/function. Example: return first4 + last4
Jigar Gor
Courses Plus Student 1,568 PointsJigar Gor
Courses Plus Student 1,568 PointsOnce the return keyword is encountered in the function, the function exits; therefore, you are only returning the first 4 instead of the last 4 - your code never reaches the line: return ABUISBEEST[6:100]. Also, you may want to reconsider the numbers used in beginning and end for the last 4. If you don't know the length of the list you're passing it may be easier to count from -1 as the starting index of the last element.