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 trialGerard Gaspard
2,473 Pointswhy 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
Geoff Parsons
11,679 PointsLooks 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.
Ho Wai Yeung
Courses Plus Student 2,010 Pointscan you please explain s = 0 and S + =x
thanks
Gerard Gaspard
2,473 PointsGerard Gaspard
2,473 PointsThanks! that solved it.