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 trial

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

Whats wrong with my code??

I passed the first_and_last_4 challenge and when I get to the next one it says something is wrong with the first_and_last_4 code. I didn't change it and I cant advance further on until its fixed

slices.py
def first_4(numbers):
    return numbers[:4]

first_4([84,684,54841,35468,354160])

def first_and_last_4(item):
    return (item[:4] + item[:-4:-1])

first_and_last_4([784,365,542,214,145,268,145,2687,123547])

def odds(odd_numbers):
    return odd_numbers[1::2]

number=list(range(30))
odds(number)

3 Answers

Steven Parker
Steven Parker
231,269 Points

That's not a correct solution for "first_and_last_4", I'm not sure why it would've passed the challenge. Take a closer look at what your slice for the "last four" is actually doing. Remember the three parts of a slice are "start", "stop" and "step".

Also, you only need to define the functions for the challenge, you won't need to invoke them (the validation does that itself).

I try and fix it and all it says is "couldnt find 'first_4' "

Steven Parker
Steven Parker
231,269 Points

When I paste the above code into the challenge, I get "Bummer: Didn't get the right values from first_and_last_4.", perhaps you made some changes since the original question?

The "Couldn't find..." message generally means there is a syntax error and so the function does not get defined.

def first_4(arg1): return arg1[:4] def first_and_last_4(arg2): return arg2[:4] + arg2[:-5:-1]

this is what I ran in my idle and it gave me the right output but on the code challenge it doensnt accept it

Steven Parker
Steven Parker
231,269 Points

It could be you're misinterpreting the objective, or your test data is ambiguous.

Try using this as a test argument: "1234testing5678". A correct implementation of "first_and_last_4" will output "12345678".