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

not working

Why ?

slices.py
def first_4:
    a = list(range(20))
    items = a[:4] 
    return items
first_4()

4 Answers

Steven Parker
Steven Parker
229,732 Points

Your slice is good.

But your function needs to take in "a" as a passed argument instead of creating it yourself using range. Just fix that and you'll pass.

Also, you only need to define the function, not call it.

Robert Young
Robert Young
1,391 Points

Explicit answer redacted by moderator

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi, Robert! I've removed the content of your answer as no explanation for how or why the code works was provided. Feel free to resubmit your answer with an explanation. This will provide the original poster with a better learning experience. Thanks! :sparkles:

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Just because it passes a challenge does not mean that the code is necessarily correct. The opposite is also true. Fully functional code may not pass a challenge because it doesn't meet a specific requirement. The challenge does not require a loop, and Steven is correct. Once a return statement is hit, the function exits meaning that it will only ever do one iteration. Also, in your code, you have (by necessity for a for loop) a variable named iterables which is never used other than to start the iteration.

Treehouse does discourage the posting of an explicit answer without any explanation as to how or why the code works. Could you please update your answer to include an explanation? Thanks! :sparkles:

Ted Runyon
Ted Runyon
1,773 Points

Also don't forget to add () when defining a function! def first_4():

Steven Parker
Steven Parker
229,732 Points

That would be part of having it "take in "a" as a passed argument". :smile: