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

Nick Walker
Nick Walker
2,561 Points

Is var1 not a global variable once it has been returned from add_list(list)?

I thought that if the value was returned, it should be able to be used in the output of the print() statement in the second task.

Here's what I came up with, that's clearly not working:

list = [1,2,3] def add_list(list): var1 = 0 for num in list: var1 = var1 + num return var1

def summarize(list): var2 = str(list) print("The sum of {} is {}.".format(var2, var1)

1 Answer

You are trying to access var2, which is within the scope of the add_list function. In order to use the value of var2, you need to call the function that returns it.

Also, the code challenge is asking you to return the string, not print it.

Try this:

list = [1,2,3]
def add_list(list):
    var1 = 0
    for num in list:
        var1 = var1 + num
    return var1

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

Hope that helps!