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 trialNancy Melucci
Courses Plus Student 36,143 PointsCode Challenge slices stage 3
Pass the first, pass the second, the following code works in PyCharm. The auto-grader keeps asking me "where's first_and_last4()?" Thanks in advance for your patient help...NJM
def first_4(iterable):
return iterable[0:4]
Ralph = first_4([1,2,3,4,5,6,7,8,9,10])
def odds(iterable):
return iterable[1::2]
Kenneth = odds([1,2,3,4,5,6,7,8,9,10])
def first_and_last4(iterable):
return iterable[0:4] + iterable[5:9]
Tom = first_and_last4([1,2,3,4,5,6,7,8,9,10])
4 Answers
Stephen Bone
12,359 PointsHi Nancy
The name of the function it wants you to define is first_and_last_4, you're forgetting to put the underscore between last and 4.
I still don't think you've got the last bit quite right as you don't know the length of the string that will be passed to the function so you can't specify it as [5:9]. You need to tell it to start from fourth last character until the end of the string.
I'm hoping that will put you on the right track but if you need any further help let me know.
Hope it helps!
Nancy Melucci
Courses Plus Student 36,143 PointsWhat can I say but "d''oh" to the first thing.... I am still struggling to figure out how to make that second part work. I'll keep working on it.
NJM
Kenneth Love
Treehouse Guest TeacherYou can get the length of an item with the len()
function. This gives you a number, which you can subtract 4 from. Or you can use the index shorthand of starting from the end of an iterable by using a minus sign (-
). So 4 from the end is -4
as an index.
Scott Davis
1,925 PointsThis worked for me
def first_and_last_4(iterable): return iterable[:4] + iterable[-4:]