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

Frank Sherlotti
PLUS
Frank Sherlotti
Courses Plus Student 1,952 Points

I'm having trouble with tasks 1-2 in functions.py. I'm completely lost.

The first answer i got was copy and pasted from some other question. But it still makes absolutely no sense to me. I basically understand that the def add_list(num_list): part is the start of a function (I think) and that's about it. The rest of it i just can't wrap my head around and it's driving me absolutely insane at this point. The second question is: Now, make a function named summarize that also takes a list. It should return the string "The sum of X is Y.", replacing "X" with the string version of the list and "Y" with the sum total of the list. Now for the second one as you can see i have no idea where to start because I just don't understand the concept. I've watched all the function videos like 4 times already and I'm still getting nowhere. Thanks for the help in advance, and sorry for the giant question.

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(num_list):
  sum = 0
  for num in num_list:
    sum += num
  return sum

def summarize()

2 Answers

Jason Zaluski
Jason Zaluski
9,412 Points

Hey, I also find the code challenges... well challenging - you are required to incorporate techniques from previous lessons... without prompting like the .format() method.

Additionally, the instructions can be cryptic initially, until you can figure out they usually are giving you clues on how to solve it.

I find the notes to be helpful (the first three lines in this challenge). These give you the input but also the expected output for each function. How you deliver the expected output is up to you to figure out - the challenge.

Copying code will solve the challenge but will not help you in the long run. Challenges get more and more difficult, and incorporate all you have learned to this point.

If you copy code, go line by line and write out what you think the line does. Checking help() on workspaces or searching online documentation will further describe what is happening. Then try it the challenge again, but with your notes only.

Hope this helps.

You are hitting a wall now, but you will feel great accomplishment when you break through!

# 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.

# create a function named add_list that returns the sum of a list *assume a list of numbers
def add_list(a_list):
  # create a variable that you will return, set it to value 0 to start
  total = 0
  # for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.
  for num in a_list:
  # += adds the num to the existing total, if this is the first then it will add to 0 (set above =0) 
    total += num
  return total

# make a function named summarize that also takes a list. It should return the string "The sum of X is Y.", replacing "X" with the string version of the list and "Y" with the sum total of the list.
def summarize(a_list):
# create the string to return, add in placeholders for X and Y
  string = "The sum of {} is {}."
# returns the formatted string with the contents of the list as well as the sum (i.e. add_list function)
  return string.format(a_list, add_list(a_list))
Frank Sherlotti
Frank Sherlotti
Courses Plus Student 1,952 Points

Thanks a lot man i really do appreciate you going into this much detail for me. It's still kind of hazy for me but i'm sure ill break through this wall soon!

Jason Zaluski
Jason Zaluski
9,412 Points

Just think that every single person who has ever learned programming was once in the exact same position you are now.

Jason, Thank you for doing this. I found this really helpful and your logic is right, getting the basics down is the most important things to to in the beginning.

Jason Zaluski
Jason Zaluski
9,412 Points

Cool thanks Nate.

I feel pressure to just get through my current task, and have to actively slow down and remind myself that my primary goal is not to finish the challenge, but to understand how to solve it.

hope you're having fun.