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

Im stuck on the slices challenge :(

Hello,

I am stuck on task 2, where it asks for the first and last 4 items in a given iterable, returned as a single value. I have attached my code, as it keeps saying the function does not return the right answer. Any suggestions would be much appreciated :(

slices.py

def first_4(numbers): return numbers[0:4]

def first_and_last_4(numbers2): numbers3 = numbers2[0:4] + numbers2[-1:-5:-1] numbers3 = ''.join(str(element) for element in numbers3) return numbers3

1 Answer

Robert Ionut Muraru
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Robert Ionut Muraru
Full Stack JavaScript Techdegree Graduate 22,194 Points

Hello,

The way I solved this one was:

  1. stored the first 4 elements in a variable
  2. stored the last 4 elements in another variable
  3. returned the concatenation (+) of these 2 variables. Don't know if this is the best way to do it but it works.