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 trialMatthew Tracy
272 Pointsfor the challenge task how would I use a for loop to sum individual items in a list? I used the sum(item) function.
see title
3 Answers
Stephen Bone
12,359 PointsHi Matthew
The way I approached this is to create a variable to hold the running total then loop through the numbers in the list adding them to the running total. Then after the for loop has finished running through the numbers in the list return the total.
def function(the_list):
total = 0
for num in the_list:
total += num
return total
I've avoided using the exact names from the challenge but it should work just the same.
Hope it helps!
Matthew Tracy
272 PointsMaybe I misunderstood the task. I ran my code in idle3 and it worked fine but I still keep getting an error on the website challenge question.
def add_list(the_list): total = 0 for i in the_list: total += i return total
def summarize(the_list): print("The sum of ", the_list, " is ", add_list(the_list))
summarize([1, 2, 3])
Matthew Tracy
272 PointsI tried the above but I keep getting the error that the return is None rather than the total. Here is my code <p> def add_list(the_list):<br> total = 0<br> for i in the_list:<br> total += int(i)<br> return total<br>
def summarize(the_list):<br> print("The sum of ", the_list, " is ", add_list(the_list))<br> </p>
Kenneth Love
Treehouse Guest TeacherYou need to return
the string, not print()
it.
Stephen Bone
12,359 PointsOK so the challenge states to assume that the list only contains numbers so there's no need to cast the items from the list into the int() function but that actually still works it's just unnecessary.
The error actually lies with the summarize function rather than the add_list function.
In the first place remember to return the string statement not to print it.
Also you have to use string formatting (as discussed in the video below) to put it together otherwise it will still fail as it doesn't return a single string. http://teamtreehouse.com/library/python-basics/ins-outs/string-formatting
It should take the format of below:
return "The sum of {} is {}.".format(the_list, add_list(the_list))
Hope it helps!
Matthew Tracy
272 PointsOh okay thank you, this is the issue.
Cody Coats
7,469 PointsCody Coats
7,469 PointsCrate a counter. You can use a
for...in
control statement. Inside the loop as you iterate over each item you can add it to the counter. No need forsum
can just use the ordinary+
operator