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

facing difficulties on this challenge

can some1 help on function challenge. challenge task 1 of 2. facing difficulty on completing this challenge

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* argum
def add-list(lst):
  result = 6
  for item in lst:

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Herbert.

Let's see if I can help a little:

Your first list of code is almost correct, but please note the the name of the function should be add_list and not add-list.

Inside the function:

where you wrote result = 6, we should instead have result = 0, as its only use it to set a variable that we will add the numbers' values later to.

Let's see how the for loop should work: we want to iterate through every single element inside the list and add its value to the result variable we just declared.

your line of code here looks of to me:

for item in lst:

Then, right below this line we need to indent as per rules and make the compiler sum up the various items. How do we do it? Please remember that we we have the item that is the value that we want to add, so we simply need to add it to the total variable, every time we iterate through an item of the list.

So, result starts form 0, then, when the for loop finds the first item, it should add it to result, making it equal to 1. The same process for the second item of the list and so on...

We can write this piece of code to make the compiler add the values:

result += item

Please note that this need to be indented in order to work fine.

In the end we need to return the result, simply writing

return result

Also, please note that this requires the same indentation as the for loop and not as return result.

Let me know if all clear

;)

Vittorio