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 (Retired) Slices Slice Functions

reza sajadian
reza sajadian
718 Points

I cant solve this quiz: Make a function named first_4 that accepts an iterable as an argument and returns the first 4 i

I dont understand why my code doesnt work for this quiz:

Make a function named first_4 that accepts an iterable as an argument and returns the first 4 items in the iterable.

I guess you have my code attached. thanks guys.

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Reza. Welcome to Treehouse.

Like Brendan said, the "Attach Code" isn't currently functioning, but we can break down the first part of the challenge (it's only two lines). First you need to create (def) a function that takes one arguement (can be named anything). Second return just the first 4 'parts' of the argument. If a string was passed in, it will return the first 4 letter; If a list was passed in, it would return the first 4 items; etc.

def first_4(item):     #creates the function  with one argument called 'item'
  return item[:4]     #returns the first 4 of the value passed into 'item'

Hope that helps. Keep Coding! :)

reza sajadian
reza sajadian
718 Points

Thanks Jason. I had made it much more complicated! Great!

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

You're welcome. And, I've done that so many times, even though simple usually works best. lol. :smiley: