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

Code works but still receiving error.

Anyone know why my code doesn't work? I tested it in the terminal and got the desired result but I'm still receiving an error on treehouse.

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

def first_and_last_4(four):
    start = four[:4];
    end = four[(len(a)-4):];
    value = start + end;
    return value

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

So let's look at that len() line.

Let's assume that four has 10 items in it. Then len(four) would be 10. len(four) - 4 would be 6, right?

But if four has 10 items in it, the indexes are 0 through 9. four[6:] would get only 3 items.

Do you remember something in the videos about negative indexes?

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are taking len(a) but a is undefined. should be your parameter. also while it is not throwing the autograder, you don't need semicolons at the end of lines in python.