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

Lianna Jarecki
Lianna Jarecki
580 Points

I get 1 when I create add_list function

I don't understand how to solve this challenge. It was not covered in the video, and I am a complete newbie.

Hi Lianna,

Do you have any code that you can post? Are you stuck with how to create the function or with the code that goes inside to add up the numbers?

3 Answers

Thomas Kirk
Thomas Kirk
5,246 Points

Hi,

The videos show you how to both create a function that adds numbers (the 1st video), and how to use a for loop (the last video). You just need to combine these two ideas. Inside your function, you can set a variable to 0, then using a for loop, take each item in the list, and add it to that variable as a running total, before returning that variable.

Thomas Kirk
Thomas Kirk
5,246 Points

Hi Lianna,

You are close. You just need to change the line doing the adding in your for loop.

At the moment you have:

 add = (my_sum + item)

This line is creating a variable called add, then assigning my_sum + item to it each time. My_sum doesn't change, and so it is always my_sum (which equals 0) + the list item value. Instead, use:

my_sum = my_sum + item

This way, my_sum becomes a new value with each iteration of the for loop - the new my_sum is the old my_sum plus the value of the list item.

Then return my_sum. You don't need to print it...

def add_list(my_num):
 my_ sum = 0
  for item in my_num:
    my_ sum = my_ sum + item
  return my_sum
Lianna Jarecki
Lianna Jarecki
580 Points

Thank you!! It finally worked.

Lianna Jarecki
Lianna Jarecki
580 Points

I think that is what I am doing, but it isn't working. Here is my code. Thanks for helping!! def add_list(my_num): my_sum = 0 for item in my_num: add = (my_sum + item) return add print(add)