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 trialAhmed Elsawey
Courses Plus Student 3,527 PointsCan someone explain the question for me please and show me how to do the answer as well because I am new to python
how can I make this function work according to the question that I have been given
# 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):
total = 0
for number in list:
total = total + number
return total
def summarize(list):
x = list.split()
y =
1 Answer
Ricky Catron
13,023 PointsStep one. Think about what you need to return from summarize. It expects a string so what I would do would be place return "The sum of [1, 2, 3] is 6." at the end of the function.
Then think about the values in that string [1,2,3] is the example list passed in, but we already have that in a variable called list so we can take out [1,2,3] and instead use "The sum of " + list + " is 6."
But now the 6 might not always be right, happily we already have a function to calculate that number for use! So what I would do next is change the 6 to add_list(list) like so "The sum of " + list + " is " + add_list(list) + "."
This should be the final answer and you wont need that list.split() or the x and y variables.
Goodluck! --Ricky
Ahmed Elsawey
Courses Plus Student 3,527 PointsAhmed Elsawey
Courses Plus Student 3,527 PointsThis is my code currently: def add_list(list): total = 0 for number in list: total = total + number return total
def summarize(list): print ( "The sum of " + str(list) + " is " + add_list(list) + ".")
But it says that the list cannot be converted to a string
Ricky Catron
13,023 PointsRicky Catron
13,023 PointsYour getting closer! Instead of printing the string you should return it. Because the challenges rely on the return to validate the answer.
Second you do not need to use str(list) when you use the + sign to concatenate it python will figure it out on its own.
Goodluck! --Ricky
Ahmed Elsawey
Courses Plus Student 3,527 PointsAhmed Elsawey
Courses Plus Student 3,527 PointsBut it says the numbers inside the list cannot be turned into a string automatically and secondly I tried returning the result it said you should print it
Ricky Catron
13,023 PointsRicky Catron
13,023 PointsWhoops you are right you do need the str and you also need it around the function call aka str(add_list(list))
I am not sure where it said you need to print it but you need to return it. This is am sure of.
Goodluck! --Ricky
Ahmed Elsawey
Courses Plus Student 3,527 PointsAhmed Elsawey
Courses Plus Student 3,527 PointsThank you very much Ricky much appreciated. How can I like add you as friend or follow you so you can help me with the python stuff if I am not sure about them?
Ricky Catron
13,023 PointsRicky Catron
13,023 PointsSadly you can't treehouse doesnt have that feature yet. But what you can do is tag me when you ask a new question on the forum. Simply type the @ symbol and the letter R a drop down should pop up and as you fill in my name it should narrow down until you see me on the list. Click me and post or save the question. It will pop up in my notifications and summon me to help.
Goodluck! --Ricky