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

The Hacker
The Hacker
20,260 Points

Some body knows what I'm doing wrong?

total =0
def add_list(items = [] ):
      total = 0
      for item in items:        
        total += item 
      print("The total is {} ".format(total))

def summarize(list_1 = []):
   total = 0
   for item in list_1:
      total += item      
   print("The sum of {} is {}.".format(list_1,total))

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Pedro,

Your code in general is valid but in the context of the challenge has an issue, you're printing strings from within your functions instead of returning them which is standard behaviour for functions. See the below which I've reformatted and cleaned up.

def add_list(items):
  total = 0

  for item in items:
    total += item

  return total


def summarize(list_1):
  total = 0

  for item in list_1:
    total += item

  return "The sum of {} is {}.".format(list_1, total)

Happy coding!

The Hacker
The Hacker
20,260 Points

Ohhhhh thanks a lot. I hate doing silly coding jaja

You could also make use of the add_list function inside summarize so that you don't have to figure out the total again.

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