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

Cheri Watts
Cheri Watts
658 Points

Make a function? Def add_list that takes a list???

I am lost. Def add_list that takes a list??? What does this even mean?

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(list)
  list.append 

That means you should pass in a parameter that is a list. Hope it helps!

3 Answers

When a function "takes a list" it means that is has an argumenbt that a user can pass in when they call that function. For example, when you type print("Hello World") what you're actually doing is calling the "print" function and passing in the string "Hello World" as an argument.

in the code

def add_list(list):

"def" defines (def is short for define) that a function is coming. the "list" in add_list(list) is an argument that a user can pass in.

So a user could do something like this

new_list = list()
add_list(new_list)

Just like you would do with print("String"), you pass in a list to your new add_list function so it can add all the elements of the list together.

When you pass something into a function, it's called passing in an argument to the function, so in this case, the new_list we passed into add_list or the String we pass into print() is an argument.

The problem wants you to create a function add_list that takes a list s an argument and then adds all of the elements of that list together.

So, if the list was [2, 4, 6] it would be added together and returned as an int: 12

Hope that helps!

Cheri Watts
Cheri Watts
658 Points

Hi David,

Sorry to interrupt you but I hope you are having a marvelous day. Are you a programmer?

I have spent the afternoon reviewing videos on Python lists, Fibonacci numbers, variables, etc from Khan Academy that are on YouTube. I am praying to learn enough computer programming to get into a, "hacker school." Anyway, do you see what the Syntax error is from the code below?

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.

var listTemps = [89, 91, 90, 87, 87]

var add_list def add_list(listTemps): result = 0 for listTemps: result = result + listTemps[:] return result

I am glad to see you're working towards further learning in programming.

A few things, for starters, in python, you don't need to type "var" in front of variable declarations. You can just do "a_variable = 10", instead of "var a_variable = 10"

as for your code

listTemps = [89, 91, 90, 87, 87]
add_list 
def add_list(listTemps): 
    result = 0
    for listTemps: 
        result = result + listTemps[:] 
    return result

Just a few things. For starters, when you call a function, as you do in line 2, you need to pass in an argument, if the function (like yours does) requires one.

So you would need to do

add_list(listTemps)

So that way the list to be added gets passed into the function.

Also, the for loop in your function isn't adding the items of the list. It's merely adding the entire list [2, 3, 4] over and over again, so you're going to end up with "[2, 3, 4][2, 3, 4][2, 3, 4]" instead of the desired int return.

In order to add elements of a list together you should do something like this:

listTemps = [89, 91, 90, 87, 87]
add_list(listTemps)

def add_list(listTemps): 
    total = 0
    for temps in listTemps:
        total += temps
    return total

So, here's what's happening. Firstly, you cannot do "for listTemps" as you aren't assigning the item in the list to a variable. Doing what we did in the revised version does that. "for temps in listTemps:" basically says, "at each step through the list, set whatever is at that index to temps" So, if we passed in the list [89, 91, 90, 87, 87], the first step through temps would equal 89, then 91, then 90, then 87, then 87.

We create a total to hold the total. "total += temps" is saying, on the first step through "total = 0 + 89" then it's "total = 89 + 91" and so on. "total +=" just means "total = total + whatever", it's a quick way to do this so you don't have to write as much code.

After it goes through the whole list, it returns the total!

Hope that helps!

Cheri Watts
Cheri Watts
658 Points

Hi David,

I am so sorry to interrupt you from your activities.

Praise the Lord!!! I am beginning to understand.

My questions is what is what to do with temp. Is it a variable? No or Yes. I say no. Is it a part of a list? No or Yes. I say yes. Truthfully, I am still lost.

listTemps = [89, 91, 90, 87, 87, 0] add_list def add_list(listTemps): result = 0 for listTemps[:] result = result + listTemps return result

What am I missing?

with your code:

listTemps = [89, 91, 90, 87, 87, 0] 
add_list 

def add_list(listTemps): 
    result = 0 
    for listTemps[:]
        result = result + listTemps 
    return result

You aren't doing what you think. What you are doing in the for loop is invalid syntax. When you use a for loop you need to have a variable before the thing you're iterating on to hold each item in the list. For example "for item in listTemps" means that, each time we go through the listTemps list, it sets whatever's in the first, second, third, and so on's posititon gets set to "item". For, the first step through, item = 89. Second step through, item = 91. and so on.

You need your code to look like this

listTemps = [89, 91, 90, 87, 87, 0] 
add_list(listTemps)

def add_list(listTemps):
    result = 0
    for temps in listTemps:
        result += temps
    return result

That should return the list added together.