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

Camilla Bendetti
seal-mask
.a{fill-rule:evenodd;}techdegree
Camilla Bendetti
Python Web Development Techdegree Student 892 Points

Slices first_4

Here's the question: Alright, these tasks are a bit harder than the others so far in this course but I know you can do them! In this first one, create a function named first_4 that returns the first four items from whatever iterable is given to it.

this is my code: def first_4(a,b,c,d,e,f,g): return first_4[0:4]

I am getting an error that says TypeError: first_4() missing 6 required positional arguments: 'b', 'c', 'd', 'e', 'f', and 'g'

What does this mean? and how do I fix the problem?

slices.py
def first_4(a,b,c,d,e,f,g):
      return first_4[0:4]

2 Answers

Sean M
Sean M
7,344 Points

Provide something to be passed through.

For example, a_list is passed into the function and will print the first 4 values.

def first_4(a_list):
    return a_list[:4]
Kent Γ…svang
Kent Γ…svang
18,823 Points

Okey, so lets take a look at what you are doing :

""" You have defined a procedure that takes 7 arguments, while the challenge is telling you that the argument is 
going to be an iterable ( f.eks a list og string ). So you only need one argument """
def first_4(a,b,c,d,e,f,g):
""" then you return a slice of you own function, this will most definitely produce an error. 
You were asked to return the first 4 items of whatever iterable is passed through the function."""
      return first_4[0:4]

So, lets look at how to solve this, here's the task we are given ;

"create a function named first_4 that returns the first four items from whatever iterable is given to it."

first we declare the function

def first_4():

It says an iterable is given to the function, this could be a list or a string for example, so lets put that in :

def first_4(iterable_thing):

Then it tells us that we should return the first 4 items in the iterable, that one you were pretty close on :

def frist_4(iterable_thing):
    return iterable_thing[0:4]

Hope this helped you a little. Have a nice day.