Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Slices Slice Functions

Kelly Vaden
Kelly Vaden
8,743 Points

Issue with Slice Functions Challenge Task 2

Hi all,

I'm unable to pass the above mentioned challenge, but am not sure why.

The prompt: 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.

My code is attached.

When I run my code in a Jupyter notebook and pass in a test list of [1,2,3,4,5,6,7,8,9,10], I do get the expected result of [1,2,3,4,7,8,9,10]. The message within the Challenge Task "Didn't get the right results" is not very helpful for debugging.

Finally, a note on the function name: If I name the function "first_and_last_4" as the prompt suggests, I get the message "first_4 not found". If I name it "first_4" I get the message "Didn't get the right results from first_4." Which makes me suspect the code checking is looking for a function called first_4.

Any advice is appreciated!

slices.py
def first_4(iterable):
    first = iterable[0:4]
    last = iterable[-4:]
    result = first + last
    return result
Kayc M
Kayc M
12,593 Points

You forgot to name the function (first_and_last_4)

def first_4(iterable):
    first = iterable[0:4]
    last = iterable[-4:]
    result = first + last
    return result

It should be:

def first_and_last_4(iterable):
    first = iterable[0:4]
    last = iterable[-4:]
    result = first + last
    return result

Nothing wrong with the code.

Kayc M
Kayc M
12,593 Points

Oh and also you can't delete the task before, otherwise, it will throw an error:

def first_4(iterable):
    first = iterable[0:4]
    return iterable 

def first_and_last_4(iterable):
    first = iterable[0:4]
    last = iterable[-4:]
    result = first + last
    return result

2 Answers

Steven Parker
Steven Parker
216,136 Points

As Kayc pointed out, it's important not to remove code from a previous task as you work on each new one. The challenge instructions even contain this reminder: "Important: In each task of this code challenge, the code you write should be added to the code from the previous task."

So at this point, you should have two different functions in the solution.

Kelly Vaden
Kelly Vaden
8,743 Points

Thanks, Kayc and Steven, for setting me straight! Clearly I misunderstood the instructions about leaving the first function as is and creating a new one for each challenge task.