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

walk me through this challenge pls. i am completely lost and frustrated.

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

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

akak
akak
29,445 Points

Hi Cheri,

def add_list(list):  #define a function that takes a list
  result = 0       #create a variable that will hold the result
  for number in list:   # loop through each number in the list 
     result += number   #add each number to the result
  return result    #return the result

It could be also simplified to:

def add_list(list):
  return sum(list)

Cheers!

Cheri Watts
Cheri Watts
658 Points

Akak,

I thank you so much for your assistance with this problem. Sadly, I have no earthly idea what you are talking about. I am very slow at computers to put the cards on the table. I am not here picking up a language on top of the three other languages I already know. I am lost at sea in the middle of the ocean trying to swim back to shore and getting very discouraged.

Thank you once again.

Cheri