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!
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
Jonathan ShullCourses Plus Student 12,739 Points
Putting the fun back in function - challenge task 1 of 2 - very lost
I am very lost, I don't feel like this question was addressed in the previous videos. I don't know how to make a function take a list and I'm not sure exactly what this question is asking me to do. Do I create a list containing 1, 2 and 3, as in: my_list = [1, 2, 3] and then create a 'for' loop as in: 'for item in my_list', and then add each number up inside the loop during each iteration? I've been spending hours on this and haven't been able to get anywhere. I saw from a previous question on this topic that you can use the built in sum() function to add up all the items in the list, but don't understand how a user function would be needed if i can just add everything up with a built-in function. Any help would be appreciated.
# 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. my_list = [1, 2, 3] def add_list(num): my_sum = int(0) for item in my_list: my_sum = my_sum + item print(my_sum)
man odell1,905 Points
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.
my_list = [1, 2, 3]
def add_list(num): my_sum = int(0)
for item in my_list: my_sum = my_sum + item return my_sum
you can print my_sum from the function or return the value then print the function using
Frederick Pearce10,677 Points
It looks like you just need to return the result of your function, as pointed out by man odell. Then, try to use this add_list function to complete the second task
def add_list(list1): tot = 0 for item in list1: tot += item return tot