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

CHINENYEM V. NWADIUGWU
CHINENYEM V. NWADIUGWU
459 Points

What am I doing wrong? i created a loop that is adding up the items in the array. then i summarize it

I created a loop that is adding up the items in the array. Then I summarize it by just printing out a string and putting in the values. but its not working.

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.

input_list = [1,2,3]

def add_list(input_list):
  total = 0
for num in input_list:
   total = total + num
return total

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

4 Answers

Okay, here is the problem. You are trying to do the whole shebang in one shot. This is a two part problem. In part on just return 'total'. Then, when you go to part 2, you will create a function named 'summarize' into which you will pass 'total'. You got it right. Just do the proper indenting.

Indent the statements inside the function, i.e.

def add_list(input_list): total = 0 for num in input_list: total = total + num return total

since there is no punctuation used for code block separation, indentation - and dedentation - is vital.

Also, according to the instructions, you don't need the 'summerize' function. just return the properly formatting string. also, the string being passed as an argument (in your code, 'input_list') is provided. You don't need to include it in your code.

Bryan Manhollan
PLUS
Bryan Manhollan
Courses Plus Student 10,096 Points
def add_list():
    input_list = [1,2,3]
    total = 0
    for num in input_list:
        total += num
    return total

print add_list()

You can change the print statement to fit you're new summarize definition. But this should do the trick.

If you need to have input_list defined outside the add_list for whatever reason. That should still work also.

input_list = [1,2,3]

def add_list():

    total = 0
    for num in input_list:
        total += num
    return total

print add_list()