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

why doesn't this code work to return the sum of the list

num_list = [1,2,3]

def add_list(num_list):
    s=0
    for x in num_list: 
        s +=x
                print(s)

add_lis(num_list)

When I run this script in OSX terminal I get the printed sum after each iteration. How can I make it print only the total sum?

2 Answers

Looks like your print statement is nested within the for loop. Try moving it back out to the same indentation level as the s= and for loop lines.

Thanks! that solved it.

can you please explain s = 0 and S + =x

thanks