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

Luke Dawes
Luke Dawes
9,739 Points

Hi everyone, I'll be honest. I'm really not clear on what I'm being asked to do with this Challenge! Could you help?

I haven't written any code in, purely because I don't even know where to start. When defining a function, should it be:

add_list()

...or should it be:

def add_list([1, 2, 3])

...like it (sort of) shows in the comments above the code? Are the instructions clear for you, and could you help me understand them?

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.

1 Answer

All functions have the syntax of:

def something(argument):

This particular function is called add_list. Many functions take an argument and that's what goes in the parentheses. This particular challenge states that you only want one argument and that your argument is a list of numbers. Assume you don't know what is in the item list and that the total is 0. Then with a for loop increment the total with the items in the list (remember that += is a good way to increment). Finally, return the total.

Luke Dawes
Luke Dawes
9,739 Points

Hi Cissie, thanks for your reply,

Here's the word-for-word challenge:

Challenge Task 1 of 2

Make a function named add_list that takes a list. The function should then add all of the items in the list together and return the total. Assume the list contains only numbers. You'll probably want to use a for loop. You will not need to use input().

I haven't yet been exposed to increments in Python (I'm currently working my way through Python Basics). and I'm wondering if there's another way to do this that allows me to stay mainly within the confines of what I've seen so far. I'm not trying to limit myself, of course, but just trying to use the tools I've been given.

def add_list(item_list)

Is this right so far?

That's right and don't forget to add a colon.

Pretend I didn't use the word increment. After you write a for loop that loops through the items in item list, add the items in the list to the total and then return that total.

Luke Dawes
Luke Dawes
9,739 Points

Hi Cassie,

I've been away from the code for a few days now, and I've come back to it with a fresher look but still not getting the result I need...

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.

num = []

def add_list(num): return num + num

...would you be able to share with me your own solution, based on the question I posted above? I think seeing the solution would shed some light on how the entire process unfolds.

No problem. Functions and loops are tricky concepts and it took me some time to wrap my head around them as well. I've included comments to explain what each line does. I hope this helps.

#define a function with an argument
def add_list(number_list):
#declare a total variable and start it at 0  
  total = 0
  #use a for loop to loop through each number in the list
  for number in number_list:
    #add each number in the list to the total
    #also can be written total = total + number
    total += number
  #return the total outside of the for loop but inside the function  
  return total
Luke Dawes
Luke Dawes
9,739 Points

Thank you, Cissie!

It does beg the question as to why the incremental features we see here haven't yet been discussed in the course, despite the fact that they're clearly necessary (based on my limited understanding, anyway). I entered your code into the exercise and it was correct, so thank you -- I understand how it works, though it isn't something I've been exposed to yet!