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 trialRussell Holz
4,811 PointsCode Challenge 2 of 2 help
I am obviously not understanding the way the question is asked, here's my code: def add_list(list): sum=0 for items in list: sum=sum+items return(sum)
def summarize(list): return("The sum of {} is {}".format(list, sum))
help please
# 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(list):
sum=0
for items in list:
sum=sum+items
return(sum)
def summarize(list):
return("The sum of {} is {}".format(list, sum))
1 Answer
Graham Mackenzie
2,747 PointsHi Russell!
Just to expand on and clarify what Nathan has said:
You don't need a parenthesis around what you are returning, so you can just delete them and return the string with the .format()
call attached.
Next, the second term of the .format()
call needs to be a call to add_list(list)
, and not the variable sum
; sum
doesn't exist as far as the second function is concerned, it exists entirely within add_list()
. Notice, however, that sending list
to add_list()
is going to return the sum
of said list, which is why it serves your purposes there.
I hope this helps! Please mark this as the Best Answer if you found it most helpful.
Be Well, Graham
Nathan Heusdens
8,806 PointsNathan Heusdens
8,806 PointsLooks like you almost had it. You should avoid using python keywords such as "list" for your variables and args. I've used test_list in place of list in your code.
for summarize you need to change to: return "The sum of {} is {}".format(test_list, add_list(test_list))