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 trial

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

Ahmed Elsawey
PLUS
Ahmed Elsawey
Courses Plus Student 3,527 Points

Can 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

functions.py
# 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
Ricky Catron
13,023 Points

Step 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
Ahmed Elsawey
Courses Plus Student 3,527 Points

This 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
Ricky Catron
13,023 Points

Your 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
Ahmed Elsawey
Courses Plus Student 3,527 Points

But 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
Ricky Catron
13,023 Points

Whoops 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
Ahmed Elsawey
Courses Plus Student 3,527 Points

Thank 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
Ricky Catron
13,023 Points

Sadly 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