Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

Cheri Watts
Cheri Watts
658 Points

Can you slowly help explaining this problem? Think brain injury slow or a baby dropped on her head at birth. That slow.

Dear Person,

I need slow simple help with this: think of a brain-injured person trying to learn computers or a baby dropped on her head at birth. Slow. Explaining everything. Regretfully, I can't use help like I already know anything about computers. I need slow simple help. Slow simple help. Picture a mother bird chewing up the worm and putting it in the baby birds mouth. That's the kind of help I need.

Thank you.

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
  for add_list 

2 Answers

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

To understand how function works in a nutshell you need to fully observe the previous class materials. I assume you do if not you need to re-watch previous video and make few examples by yourself.

Here's my explanation with the attempt to not use any jargons and assuming you are having very hard time understanding this concept. Let's plan what we need first.

  1. A function named add_list.
  2. The function should take a list <<< this means our function need a space to remember what you gave to function.
  3. Go through list that you passed and add all of them. a. We will do this with 'for ... in ...' loop. b. We also need a space to store our result.
  4. Finally, function need to tell us what it did for us.
1 def add_list(my_list):    
2    result = 0

3    for element in my_list:
4       result = result + element
5    return result

Here's what each line does.

1 def <- define a function named add_list which have storage called my_list.

2 we want to save our result somewhere else because result will not create itself.

3 for .... in ... loop goes over a list. In this case, It will go over my_list and save each item in space called element.

4 We update our result by adding it with element that is an item from my_list. for...in... loop will run until it has gone through all the items.

5 At this point, for...in... loop ends and we want our function to give that result to us and our function is done!

If you don't get it comment which part that you didn't get.

Cheri Watts
Cheri Watts
658 Points

Gunhoo,

Thank you my Hero!

You may think I am hyperbolizing but what you provided for me was a sense of confidence. I slowly...slowly...ever so slowly read your answer and I understand it. The parentheses after add_list infuriated me because I kept saying to myself, a list has square brackets! A list has square brackets!!!

Gunhoo, I literally forgot that functions have parentheses for the argument.

I am going to tiptoe over to the Challenge and using you clear instructions, get started. My question is, do I need to define the variables add_list and my_list? I believe I should do that above the statement below:

Def add_list(my_list):

I appreciate your help! I will be asking more questions. Here is another question: is element a variable that needs to be defined.

I hope you are having an awesome day!

Cheri

Cheri Watts
Cheri Watts
658 Points

Gunhoo,

Thank you my Hero!

You may think I am hyperbolizing but what you provided for me was a sense of confidence. I slowly...slowly...ever so slowly read your answer and I understand it. The parentheses after add_list infuriated me because I kept saying to myself, a list has square brackets! A list has square brackets!!!

Gunhoo, I literally forgot that functions have parentheses for the argument.

I am going to tiptoe over to the Challenge and using you clear instructions, get started. My question is, do I need to define the variables add_list and my_list? I believe I should do that above the statement below:

Def add_list(my_list):

I appreciate your help! I will be asking more questions. Here is another question: is element a variable that needs to be defined.

I hope you are having an awesome day!

Cheri

Cheri Watts
Cheri Watts
658 Points

Gunhoo,

Thank you my Hero!

You may think I am hyperbolizing but what you provided for me was a sense of confidence. I slowly...slowly...ever so slowly read your answer and I understand it. The parentheses after add_list infuriated me because I kept saying to myself, a list has square brackets! A list has square brackets!!!

Gunhoo, I literally forgot that functions have parentheses for the argument.

I am going to tiptoe over to the Challenge and using you clear instructions, get started. My question is, do I need to define the variables add_list and my_list? I believe I should do that above the statement below:

Def add_list(my_list):

I appreciate your help! I will be asking more questions. Here is another question: is element a variable that needs to be defined.

I hope you are having an awesome day!

Cheri

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Thanks for sweating it. I guess my challenge to explain this concept without making too complicated sort of paid off.

//Don't forget to use def not Def
def add_list(my_list):

The question was do I define variables add_list and my_list. Variables are something that hold a piece of data which we can refer to it later. The way we refer to it is to initialize name and call its name. So apply this concept to what you've asked.

1 You declared your function and gave it a name add_list to be accessible later on.

2 You initialized my_list to be available for our function which currently is just a name.

3 So you are sort of declaring variables already.

One thing that is helpful to know is the scope of variable. The scope means a place which we can access to variable.

For example, when we try to use our my_list outside of a function. You will get an error because my_list variable is only available to its inside not outside. The outside we call it global that will mostly be the 0 indentation level. The inside is called local which is anything that is inside our add_list function on this case. Here's example.

sample_list = [1,2,3]

def add_list(my_list):
    //Things inside this function is considered local.

The sample_list and add_list function is in the global scope which has no indentation.

Things inside global is always available for you while things in local is only available within themselves.

That's why we need a return statement at the end otherwise the function will just throw the result away by the time it reaches the global scope.

Cheri Watts
Cheri Watts
658 Points

Hi Gunhoo,

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

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

It's probably better if you opened up in another post since this post is getting big. Then you may ask me using one of teamtreehouse feature.

First to answer your question I'm not a programmer, not yet and I'm just a self-learner right now. And my knowledge in Python is bare minimum since I'm trying to learn JavaScript for the most part.

Second your programming question.

Are you trying to add all the item in the list?

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

//You don't need this since this variable is unused.
var add_list 

def add_list(listTemps): 
    result = 0

    //1 To go over each item in the list you need for .... in .... statement.
    for listTemps:
        //2 I don't know why you are using listTemps[:] but this won't help adding the result.
        result = result + listTemps[:]
    return result

1 for .... in .... statement works as follow

for variable that each item in the list will bind to in the list.

For example,

my_list = [1, 2, 3]

//At each iteration, the item will bound to an element in my_list. Starting from index 0 to 2 in this case.
for item in my_list:
    print(item)

2 list[start: end] syntax is used to pick individual or part of items in the list.

Doing something like list[:] will be just a list because omitting start and end will result referring to whole.

So to finish add_list part the process is same as your original question.

def add_list(li):
    result = 0
    for element in li:
        result += element //This is shorthand for result = result + element
    return result

I highly suggest you take your time to familiarize yourself with syntax before you engage into problems like Fibonacci which taught using recursive definition.

Your best friend is probably print() which allows you to see how program treats your thought.

Python is one of those languages that you should absolutely need to install in your computer rather than relying on this site's workspace. Because things that ships with Python both works as real time interpreter and text editor.

The order of the functions doesn't matter because the will be called from latter on in the script. Using a line like so:

print(summarize([1, 2, 3]))  # This line will only be needed to you to test in workspaces / your terminal 

The function (add_list) is just adding all the values in the list. You could use a 'for loop' for this as you started out here but I would recommend using the build in list method 'sum'. To do so it would look something like this:

def add_list(some_cool_list): 
    return sum(some_cool_list)

Next you will want to create another function (summarize) this is also very straight forward.

def summarize(yet_another_cool_list):
    return "This sum of {} is {}".format(yet_another_cool_list, add_list(new_list))

Let me know if you need a more step-by-step.

ps:

The script should look in whole like this:

1.   def add_list(some_cool_list): 
2.       return sum(some_cool_list)
3.
4.   def summarize(yet_another_cool_list):
5.         return "This sum of {} is {}".format(yet_another_cool_list, add_list(yet_another_cool_list))
6.
7.    # This line is only needed for testing
8.   print(summarize([1, 2, 3]))
  • 1: Creating a function that takes one argument (the variable name can be anything name it something reasonable)
  • 2: The returns the some of the variable is being passed. (one note is if you pass it a value that does not have the sum method you'll get a runtime error (a TypeError))
  • 4: Creating a function that takes one argument (the variable name can be anything name it something reasonable)
  • 5: This is another simple return, but there is some other things going on here.

    a. This return is returning a string
    b. The make up of this string is " test {placeholder} text {placeholder}"
    c. On the end of this string you have a string method that fills the placeholders (the string method is .format())
    d. The variables being passed in the the format method are the summarize argument variable (yet_another_cool_list) and the return value of calling your function add_list with your yet_another_cool_list variable as the add_list argument variable. When the computer reads this it will call in all the values and run them in as it needs them. (This is a high view of what is going on there.)

Cheri Watts
Cheri Watts
658 Points

Carl,

Thank you. Thank you. Thank you for responding.

Blessings!

Cheri

PS: Thank you once again.

Cheri Watts
Cheri Watts
658 Points

Hi Carl,

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? We have to use a for statement.

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

Cheri the code is a little hard to read with in all inline like so in the future it makes it much easier to view code in this context by putting in in a code block in markdown. This is done by putting three ``` fancy quotes before the start of your code sample and three at the end.

With that said, it looks like your for loop is missing a variable. for loops generally look like:

for iterating_var in sequence:
   statements(s) # or some code doing things

So for a basic example:

my_name = "Cheri"

for letters in my_name:
    print(letters)

(This would print to the terminal each letter in Cheri on a new line) Here it shows you the basic setup for a for loop.