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

Andy Bosco
PLUS
Andy Bosco
Courses Plus Student 681 Points

I'm stuck.

I can't seem to figure out what I did wrong. See functions.py for my code. I keep getting this message "Bummer! summarize returned the wrong output. Got 'The sum of 6 is 6' instead of 'The sum of [1, 2, 3] is 6.'."

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(num):
    total = sum(num)
    return total

def summarize(fun):
    num = add_list(fun)
    total = add_list(fun)
    return "The sum of {} is {}".format(num,total)

3 Answers

Mari Johannessen
Mari Johannessen
12,232 Points

Hi Andy! In the last line in your last function you're formatting 'num' and 'total' and they are both calling the same function in the last function, so they will give the same output. If you change that to the value that's given to the summarize function, you will see that the code now works! Hopefully I explained it good enough for you to understand :)

def add_list(num):
    total = sum(num)
    return total

def summarize(fun):
    num = add_list(fun)
    total = add_list(fun)
    return "The sum of {} is {}".format(fun,total)
Andy Bosco
Andy Bosco
Courses Plus Student 681 Points

Hi Mari, thanks for the quick respond. I changed my last line to "return "The sum of {} is {}".format(add_list(num),summarize(fun))" and I'm getting this error now "Bummer! TypeError: 'int' object is not iterable"

Mari Johannessen
Mari Johannessen
12,232 Points

Hi again,

Try changing it to 'return "The sum of {} of {}".format(fun,total)'. You don't need to call the function again inside the function. You're already calling add_list(fun) inside the function which will be assigned to the variable 'total', so this variable is ready to be used in the return statement.

Take notice that the variable that has the wrong value ('num') is being assigned the value of add_list(fun), which returns the sum of the list. The error you are receiving is asking for the list itself. So instead of passing the 'num' into the 'format' method, try passing the name of the list ('fun').

Andy Bosco
Andy Bosco
Courses Plus Student 681 Points

Hi Gene, I'm new to Python and don't understand what do you mean by "So instead of passing the 'num' into the 'format' method, try passing the name of the list ('fun')."

String are objects. Objects are entities that have methods that access or change their values. A method is simply a function that is attached to the object. When you type '.format(var) ' onto the end of a String, you are calling the String's 'format' method.

if you change your statement to ' return "The sum of {} of {}".format(fun,total) ' The method is simply changing the value of the String by replacing the {}'s with the values you choose. In this case, you use the variable named 'fun' because it is wanting you to display the whole list in place of the first {}.