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

How do you add numbers in a string?

I'm trying to learn Python and I do have other language experience, but I'm having problems getting my list of numbers to add together to make a sum. Here's my code at this point:

num_list = []

def add_list(num):
  for num in num_list:
    total_num = num
    return((total_num + num))
    continue

while True:
  num = input("")

  if num == 'done':
    break

  num_list.append(num)
  print(add_list(num))
  continue

This is for a challenge in the Python Basics course and I've been working on this for the past few days. I'm just so lost. Any advice?

Hi Ben,

See Ratik's answer and what I think should be corrected.

It seems as if you're trying to do too much here and write a complete program rather than just the function definition.

Don't worry about the list itself or calling your function. The challenge will call your function for you and pass in a list when testing.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I edited your post for proper code highlighting.

3 Answers

Ratik Sharma
Ratik Sharma
32,885 Points

The question in code challenge is as follows if I'm not wrong: Make a function named add_list that adds all of the items in a passed-in list together and returns the total. Assume the list contains only numbers. You'll probably want to use a for loop. The solution to this is as follows:

# Here we're defining the function the takes a list (the list only needs to have numbers as per the question so no need to convert strings)
def add_list(numbers):
  # We initialise a variable 'total' to 0. This is our summing variable. 
  total = 0
  # We start our for loop going through each number in the list and adding it to variable total
  for number in numbers:
    total += number
  # After adding up, we return total 
  return total

# This is the second function we need to define which calls the add_list function to summarise 
def summarize(list):
  # Using a return statement is enough here (as per the challenge). Note that we use call add_list from within format() 
  return "The sum of {} is {}.".format(list, add_list(list))

Note: Be careful about the indentation while typing out Python code. If this helps, mark it as a top answer so others can refer to it!

Hi Ratik,

You should use a different variable name and not list

In your for loop, number is a number from the list so you should just add it to your total

total += number

You're using it to access the list at a particular index and this will probably lead to accessing the list out of bounds.

Thank you very much! This makes so much sense now. :D

You can convert strings into integers by using int(). So in your case, when you append num to the list, you'd just do num_list.append(int(num)).

Thomas Ton
Thomas Ton
9,836 Points

This is how I did it. Hope it helps!

def add_list(some_list):
    return sum(some_list)

def summarize(another_list):
    return "The sum of {} is {}".format(another_list, add_list(another_list))