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

first_4 runs in workspace "Bummer! Try again!" in the challenge

the task is to make a function that accepts an iterable and returns the first four of that iterable. I have used workspace to test my syntax before putting it into the challenge. The following works there but gets "Bummer!" when in the challenge.

At heart the code is

    def first_4(): 
      return(temp[:4])

When temp is set by

    temp = input("What to truncate? ") 

or by

    temp = list(range(8))
    print(first_4())

successfully returns the first four elements of the string or list (because both are iterable). When the heart of the code is put into the code challenge, "Bummer! Try again!"

2 Answers

Dan Johnson
Dan Johnson
40,532 Points

The challenge wants you to have a function that will accept any iterable and return the first four elements of what is passed in. You just need to have your function accept an iterable as an argument. The value assigned to the argument will be supplied when your code is being checked.

I believe that I have got a function that accepts an iterable as an argument. I passed it a list and a string and got the expected result in workspace which suggests that the function works as requested. However the challenge is kicking my answer out as incorrect.

I think what is happening is that there is some language subtlety in the challenge that my "haven't done this since BASIC and Pascal" mind is missing.

And this morning it works after explicitly naming the function input as temp (which I had done while bashing about last night and gotten "Bummer!").

Dan Johnson
Dan Johnson
40,532 Points

Sounds like you got it, but just to clarify all the first challenge is looking for is something like this:

def first_4(iterable): 
  return iterable[:4]

No other code is required.