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

Martin Heinvee
Martin Heinvee
872 Points

Task about using slices-solution not passing the challenge.

In python collections course I am stuck with the following task:

Challenge Task 2 of 4: 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 solution gets a 'bummer' for the following solution: If item2 = 'kukeleegu' then my solution gives 'kukeugee' and if item2 = [1,2,3,4,5,6,7,8,9] then 12349876. I believe that these are correct answers and solution should be bassed but I will get a 'bummer', why ? Can anyone help me with that ?

def first_and_last_4(item2):
    uus=[]
    for i in item2:
        character=str(i)
        uus.append(character)
    string1 = ''.join(uus) 
    return string1[0:4:1]+string1[-1:-5:-1]

2 Answers

Steven Parker
Steven Parker
229,004 Points

It looks like you're reversing the order of the last 4 items, but the instructions don't ask for that. Just return the first and last 4 items in their original order.

Martin Heinvee
Martin Heinvee
872 Points

Thanks for the comment Steven! Yes the last four were backwards, but still the solution is not passed. I re-posted the issue in a better form.