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 trialanoushkaprashanth
664 PointsHow do you add all the numbers in the list to get a total?
I do not get how to do this challenge. Please help!
# 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(num):
for num in add_list:
add_list.append(num)
total = num + num
return(total)
3 Answers
Matthew Rigdon
8,223 PointsTwo things I would note:
First, you want to return(total) on the same line as your For loop. Otherwise, your function sends this value before it ever loops a second time.
Second, just focus on adding the current num value to total. Before you can do this you must set total = 0 before the For loop starts. I would structure it like this:
def add_list(input_list):
total = 0
for num in input_list:
total = total + num
return total
Spend some time reviewing my code and see if you can make sense from it. Also, it is helpful to visual what is happening, which is possible at: www.pythontutor.com
Good Luck
Matthew Carr
11,220 PointsFirst try giving total
a value before the for loop starts. Also num + num just adds the value to itself, and I don't think you intend to just multiply the number by two each time.
anoushkaprashanth
664 PointsThanks! I will definitely try these out.