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

My code for challenge 2 won't pass. Why?

So I don't really understand why my code won't pass or how to make it pass. Please help.

~Ryan

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 (list1):
  total = 0
  for i in list1:
    total = total + i
  return total

def summarize (list1):
  return "The sum of {} is {}.".format(list1,total)

2 Answers

You're almost there!

Your second function can't "see" the total value from your first value. Any variable that you declare inside of a function is only used inside that function and nowhere else.

So in your section function you should make another total variable that's equal to the total of the list you've passed to it. You can use the first you've written function to do this, like this:

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

Calling functions from within other functions is a very common programming technique and it's a great way to make your code more organized and easier to read.

Thanks Hayley, I can't believe I forgot that I could just call the function inside my second function! Also thanks for such a quick reply.

In your code, the total variable is a part of the add_list function and is not accessible inside summarize. The way you have to do this is to call add_list from summarise and pass it an argument. Hope this helps!