Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

Christopher Miller
Christopher Miller
614 Points

Hitting brick wall on Python Functions

I'm hitting a brick wall on Python Functions and I've been on this one for about a week now.

I can't even get Check Work past my first line of code and I continue to get the following:

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

I have the first line written as:

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

I don't understand where this is going wrong.

I'm at the point where I'm copying someone's else program in an attempt to figure this out, and its still not working.

functions.py

add_list([1, 2, 3]) should return 6

summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."

Note: both functions will only take one argument each.

```def add_list([1, 2, 3]): total=0 for i in add_list: total = total + 1 return total

def summarize(add_list): return "The sum of {} is {}.".format(str(summarize), str(add_list(summarize))

4 Answers

Hi Christopher!

First off, you need to provide a variable for the add_list function instead of the actual list. For example, to get past the first task in that challenge, you would want to do something like this:

def add_list(my_list):
  total = 0
  for item in my_list:
    total += item
  return total

See how we have a variable there in the add_list function instead of the actual list?

Try working from there and see if you can get the second task.

Christopher Miller
Christopher Miller
614 Points

Ok, I get the the 1st task done, however...I'm just copying your code and running it. Not really understanding the why. The greyed out code has a list as an example. Don't I want to use Python list with it?

However I'm already stuck at the 2nd task with a syntax error.

I've set the 2nd function, def summarize(my_list). Is this correct? Do I create a new argument with the same name?

def summarize(my_list): print("the sum of {} is {}".format(len(my_list)):

The variable "my_list" is a stand-in for any list that could be provided for the function. So, if someone was using your function, they would call it like "add_list(my_list)" (prefaced with "my_list = [1, 2, 3]") or even more specifically "add_list([1, 2, 3])". Either way would work, because the variable is just a stand-in for whatever is actually provided when calling the function. Does that make sense?

I'll look at your second function in a moment...

Here is my code for task 2:

def add_list(my_list):
  total = 0
  for item in my_list:
    total += item
  return total

def summarize(my_list): 
  return "The sum of {} is {}".format((str(my_list)), add_list(my_list))

For this task, you can go ahead and use the same list variable. What I did different here is that I want to return the value of the function instead of printing it to the console/screen. In addition, the task is asking for the first replacement to be the string version of the list (str(my_list)) and the second replacement being the total of the summed list, where you can actually just use the function you wrote in task 1 (add_list(my_list)). Make sense? Let me know if you have any questions!

Christopher Miller
Christopher Miller
614 Points

I got it to work, however I just copied your code to get it to work.

For some reason, I can't code it up from my head, however if I see someone's else's work, I get it. I don't understand why.

I kind of wish there were more examples that we could do to build up to the challenges.