Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Challenge 2 of 2 in python basics

Q- Now, make a function named summarize that also takes a list. It should return the string "The sum of X is Y.", replacing "X" with the string version of the list and "Y" with the sum total of the list.

I think the code challenges should be above reviewing what we've learnt. In task 1 it was to 'add the items of a list' which we haven't even learnt about. Can someone please explain the task here? Thanks in advance !

functions.py
def add_list(list):
    sum = 0
    for items in list:
        sum = sum + items
    return(sum)
def summarize(list):
    x = ''.join(str(e) for e in list)
    y = 0

2 Answers

Boris Ivan Barreto
Boris Ivan Barreto
6,837 Points

Hello Pandey,

Some tips for this challenge, if you need to increase or decrease the value of your variable you should use the follow: variable += something > this is equivalent to sum = sum + items.

You can also use the str() function to change the list into string.

Have a look my code

def add_list(items):
    sum = 0
    for item in items:
      sum += item
    return sum

def summarize(my_list):
    return "The sum of {} is {}".format(str(my_list), add_list(my_list))
Matthew Rigdon
Matthew Rigdon
8,223 Points

Here is the way that I solved it, which in my opinion is simpler and uses more of the class's knowledge:

def add_list(list):
  sum = 0
  for items in list:
    sum += items
  return sum

def summarize(list):
  sum = add_list(list)
  return ("The sum of {0} is {1}".format(list, sum))