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

Greg Kitchin
Greg Kitchin
31,138 Points

Putting the 'Fun' into 'Function' task 2. Help?

So I'm getting an error message saying that task 1 is no longer passing. I've not changed it from the first task. Does anyone know where I'm going wrong? I think I've done something wrong with calling add_list in the second task, but not sure.

//Task 1 lst = [1,2,3]

def add_list(lst):                                  
    total = 0                                       
    for number in lst:                              
        total = total + number            
    return total

//Task 2
def summarize(lst,sum):                             
    add_list()                                      
    list_to_string = str(lst)                       
    print("The sum of {} is {}.".format(list_to_string,total))

2 Answers

The function summarize only needs one argument, the list that you want to "summarize". Instead of using sum as a parameter, you can use it as a variable within the function to store the value that is being returned from calling the add_list function (also remember that you need to pass in a list to the function: add_list(lst). The way you have it right now, add_list is being called and returning a value, but nothing is being done with that number. total is a variable that is local to the add_list function, and it can't be called outside the function definition. Also, you might need to return the string instead of printing it.

Greg Kitchin
Greg Kitchin
31,138 Points

That helped a lot, thanks.