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

Task Breakdown for add_list

Having a hard time understanding this challenge for some reason. If possible, can someone who can do this one easily, possible submit a screenshot---- and comment each line as to what each command and basically as to what the function is doing?

2 Answers

Hi Daniel,

Hehehe is weird writing "Hi Daniel" we have the same name.

Ok, so here is there answer explained, I hope this help you and ask me again if something is not clear.

def add_list(my_list):  # This line define a function with an argument, 
                        # and that argument is a list. It could be any name.

  sum = 0 # Because we are looking for the sum of all the items in the list,
          # I created this variable to hold my sum. 
  for item in my_list: # This for loop iteration trough the list. Each time it is
                       # a different item, so in the next line I added.

    sum += item # Here, like I said, I added to sum. For example if my_list[0] = 4,
                # then after I added item to sum, sum=4

  return sum # After the for loop ends, in other words, after there are not items
             # left in my_list, I returned the sum.


# This function is pretty much the same until the return keyword
def summarize(my_list):
  sum = 0
  for item in my_list:
    sum += item
 # The only difference here is that I am returning a String instead of an integer. 
# Like the function above, I added all the item to sum, then I returned a String using my_list and sum.
  return "The sum of {} is {}".format(my_list, sum)

This is the solution without the comments

def add_list(my_list):
  sum = 0
  for item in my_list:
    sum += item
  return sum

def summarize(my_list):
  sum = 0
  for item in my_list:
    sum += item

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

I hope this help. Let me know if you have more questions.

-Dan

Hey Daniel,

Thanks for that well explained solution, as it's exactly what I was looking for. One other thing, that I think makes a big difference is the indentation and spacing. Like, aside from defining the function, when and where do you indent for "sum" and "for"? I see on the first portion, you put the return statement right below sum, but on the second portion, you had a line break--- does that ultimately matter? Is there a specific set of rules to follow with indentation and line breaks?

Thanks!

Hey again,

Well, Python does care about indentation. You have to indent a lot, and indentations create blocks. For example, in Java Script I would write some like this and it doesn't matter.

if( my_list.length() == 4 ){
   console.log("The length is 4!");
       var new_list;
     var other_list;
}

Don't get me wrong, this is a really bad practice. It makes your code less readable, and trust me you want it to be as clear as possible. But the end it works.

All the opposite to Python, space and indentation matters. At first is kind of weird though, but you'll get it.

if my_list.len() == 4:
       print("The length is 4!")
       new_list = []
       other_list = []

Regarding the return after sum, it doesn't matter because Python doesn't care about new lines. But there are certain coding style that you should follow though, you don't want to have your code full of new lines.

You can do it both ways, without the space between, like this:

def summarize(my_list):
  sum = 0
  for item in my_list:
    sum += item
  return "The sum of {} is {}".format(my_list, sum)

I hope this helped.

-Dan

I'm confused about the sum += item part. I had sum = item + sum in my loop and it didn't work. Can you explain?

I would love to! Can you post your entire code, is you don't mind?