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

Tristan bradford
Tristan bradford
1,123 Points

I don't understand what they're looking for

I know my code will work in the way they asked, but I'm not sure how to go about this problem using loops, or I don't know what answer they're looking for.

num_list = list([1, 2, 3])
add_list = sum(num_list)
summarize = "The sum of " + str(num_list) + " is " + str(add_list)
Tristan bradford
Tristan bradford
1,123 Points

Thank you for the fast responses everyone. I get what they're going for now, and I'll give it an attempt in a few hours when I get home.

3 Answers

Zachary Hudson
Zachary Hudson
Courses Plus Student 12,154 Points

I'm going to let Kevin break this down if I did it wrong, but this passed for me after retrying.

def add_list(list):
  total = 0
  for number in list:
    total += number
  return total

def summarize(list2):
  total2 = 0
  for number in list2:
    total2 += number
  list2string = ''.join(str(list2))
  return "The sum of {} is {}".format(list2string, total2)

So the real issue here is that your method is very rigid. Kevin is trying to teach you how to make functions so that when a user calls them and chooses a random set of numbers, it will still calculate the same way. Your solution works, but only with those sets of numbers and can't be reused.

add_list: so...what I do is make a new function called add_list. It takes a list as the argument. It starts out with a "counter" of 0. then it uses a "for" loop to go through each number in the list and add it to the total. It then returns the total.

summarize: This one is similar. I set a total counter again. I loop through each number in list 2. Add them to the total. Now that the function is done using list2 variable...I convert it into a string using the .join and str function. Finally, I return my newly formatted list2string and total2 counter.

Hi Tristan

The Challenge asks you to create a function named add_list. This function should take a list as an argument ie [1,2,3] and return the total of [1,2,3] in this case 6. You will need a variable to hold the total value, you will also need a for loop. this is how you write a for loop

list = ["hello","Tristan"]
for word in list:

# output would be

since we have 2 values in our list the loop goes round list twice printing word each time.

First of all, they're asking you to create functions not variables. So your first function you would create would be:

def add_list(num_list):
  #since it says to use a for loop we'll do it that way
  total = 0
  for num in num_list:
    total += num
  return total

and then you'd have another function called summarize:

def summarize(num_list):
  # do some work  in here using the function add_list to format the string
  return some_formatted_string