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 trialBrandon Adamson
934 PointsI 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
Treehouse Guest TeacherPay 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.
Brandon Adamson
934 PointsBrandon Adamson
934 PointsDoes that mean the first line will look like this?
def add_list([1, 2, 3]):
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherBrandon Adamson no, you need to give it the name that you want the passed-in value to be called inside the function. Consider:
When
add_to_2
is called, you have to give it an argument. Inside ofadd_to_2
, the argument that you gave will be callednum
.So you need to give your list a name, like, say,
my_list
orlist_to_add
or something.