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

SONIKA JAIGANESH
SONIKA JAIGANESH
533 Points

How to complete 'functions' on python basics track?

I'm on the second part of the 'functions' challenge and I'm a bit stuck… apparently treehouse is 'not getting an output from the summarise function? The challenge is: 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.

help is appreciated!

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):
  return list[0] + list[1] + list[2]

def summarize(list):
  return list[0] + list[1] + list[2]
  print("The sum of {} is {}".function(str(list),add_list))

1 Answer

Hi Sonika, One of the problems is that as soon as your program hits the return statement in the second function, it leaves that function. This always happens with return statements. Functions will not execute code that comes after a return statement, in this case the print statement.

Also, your formatting in the print statement requires the use of the .format method instead calling a function. There is no need to add the numbers in the list individually in your summarize function since you have already created a function that does that. You're on the right track with the idea of calling the add_list function, but it needs to be passed in as the second argument of the format method. Here is some code to illustrate what I mean:

def add_list(list):
    return list[0] + list[1] + list[2]


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

if you want to test the code, you can create a variable which holds an array of three items and then call your summarize function in a print statement, like so:

my_list = [1, 2, 3]


def add_list(list):
    return list[0] + list[1] + list[2]


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

print(summarize(my_list))