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 Basics (Retired) Putting the "Fun" Back in "Function" Functions

I have no idea how to do this

TypeError: add_list() takes 0 positional arguments but 1 was given

def_addlist():
  num_list = [1, 2, 3]

Someone please tell me how to do this

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Pay attention to your syntax. def is the keyword to create a new function. There's a space between it and the function name. Inside of the parentheses, you specify any arguments that the function takes. Your function should take one argument, which'll be a list. You don't have to create num_list yourself.

Does that mean the first line will look like this?

def add_list([1, 2, 3]):

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Brandon Adamson no, you need to give it the name that you want the passed-in value to be called inside the function. Consider:

def add_to_2(num):
    return num + 2

When add_to_2 is called, you have to give it an argument. Inside of add_to_2, the argument that you gave will be called num.

So you need to give your list a name, like, say, my_list or list_to_add or something.