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

David Rivier
PLUS
David Rivier
Courses Plus Student 1,235 Points

Code working in Workspace but not in the challenge section

In the challenge section, I received the following error message: "Bummer! summarize returned the wrong output. Got 'None' instead of 'The sum of [1, 2, 3] is 6.'."

After a few tries, i decided to try this code in the workspace and find out that it is delivering the expected output.

I would appreciate your help on this, as I'm stuck ;)

here the code:

numbers = []

def add_list(numbers):
  list_sum = 0
  for x in numbers:
    list_sum = sum(numbers)
    return list_sum


def summarize(numbers):
  str1 = str(numbers)
  str2 = str(add_list(numbers))
  print ("The sum of " + str1 + " is " + str2 + " .")

add_list([1, 2, 3])
summarize([1, 2, 3])

Thanks.

Maxim Andreev
Maxim Andreev
24,529 Points

Use

###my code here

to show your code in the forum otherwise it's too convoluted for us to look through it.

6 Answers

Khaleel Hamid
Khaleel Hamid
5,258 Points

Yeah I forgot to tell you didn't need the print all you need was return "put your string here" that would've done the trick.

Glad you got it working tho :)

David Rivier
PLUS
David Rivier
Courses Plus Student 1,235 Points

Hi Guys,

Thanks for your help. I rewrote the code with your help. Indeed, it was much better and clearer; although I kept returning to the same error message. At the end, instead of using a "print" in the second function, I ended up putting the string into a variable and use "return" and it made the trick.

Thanks again,

David

Hey David - That's happened to me a few times on different exercises. You pass the first task just fine and because of an error in subsequent tasks it says that task 1 is now failing. Once you fix the error of the task you're working on when you get the error message, its usually okay.

So i noticed a couple of things:

  1. it looks like in your add_list() function, even though you're looping through for each of the numbers in your list, you're just summing the entire list each time. Also, your return gets repeated for every iteration of your loop (indentation mishap) - should accomplish what you're looking for but it's not what you're aiming to code. You can either use sum only or use another variable to sum up each of the numbers of the list in your for loop - you're trying to do both right now

  2. There's no need to try to explicitly turn the returned value from "add_list()" into a string. You can just use the add_list() function only. Also, you can reduce your code overhead by skipping the assignments of str1 and str2 and just run str on "x" in your print statement.

Hope this helps.

Best of luck

Kurt

Khaleel Hamid
Khaleel Hamid
5,258 Points

I believe your return should be outside the for. Also you haven't declared a sum variable and are calling it and missing the + by the = in order for it to add the numbers in the list.

I had this and it worked

def add_list(items):
  total = 0
  for num in items:
    total += num
  return total

You should try this

def add_list(numbers):
  list_sum = 0
  for x in numbers:
    list_sum += x
  return list_sum
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I don't think I'll ever create a Code Challenge where you have to print() something.