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

Why return but not print?

For my original code, I was stuck for a while receiving the error "Bummer! summarize returned the wrong output. Got 'None' instead of 'The sum of [1, 2, 3] is 6.'."

my_list = [1, 2, 3]

def add_list(my_list):
  total = 0
  for num in my_list:
    total = sum(my_list) 
  return total

def summarize(total):
  print("The sum of {} is {}.".format(my_list, add_list(my_list)))

After changing the last line from print to return, it worked just fine. Why did return work in this case, but print did not? I'm still working out the differences between print and return it would seem, and though I think I understand them with numbers, I'm not sure I understand with strings and variables. I'm thinking it might be related to using the function add_list inside of print.

Thanks in advance!

3 Answers

The difference between return and print is that return sends back data to where the function was called. Print sends output to the console.

Example of return:

def test():
    return "Hello there"

string_variable = test()

Nothing will be printed in the above example because print is never called. The function test returns a string but it can be stored in a variable instead of printed.

def test():
    print "Hello there"

test()

This function will print "Hello there" to the console because print is called in the function. It is recommended not to print in functions most of the time, there are dozens of exceptions to this rule but in practice it is better to do:

def test():
    return "Hello there"

string_variable = test()
print string_variable

# or even 

print test()

I think the reason Treehouse gave you an error is because your function didn't return anything to their testing code it simply printed and their testing code assumed nothing happened because nothing was returned.

If you have any more questions feel free to ask! --Ricky

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yep! I can't test values I don't get and the way I get them is with return.

Thanks Ricky, that example was very easy to understand and cleared up my confusion perfectly!

Glad to help! I would reccomend messing around in workspaces a little just creating your own basic programs to hammer home those fundimentals. It will come in handy down the road when more complex topics come up.

Goodluck! --Ricky

It is working for me. Check the code inside your terminal. A lot of the times, teamtreehouse looks for specific ways to do something, and may print out invalid errors. However, it's good practice to return values for functions. When a function can't return a value, it puts out None, which I think that the teamtreehouse console thought.

Not too sure though as to why it returned None though, as I don't know the theory behind it much. But the code works.

I wasn't getting an error in the treehouse workspace, but it also wasn't displaying anything than the next command line, so I figured something still had to be off. The None response is good to have defined though, thanks for your help!

Also, in your for loop, you have a variable num that you are not using, since you are using the built-in sum function.

So you could simply do:

def add_list(my_list):
  total = sum(my_list) 
  return total

Right, thanks for pointing that out! If I did want to use the variable num (and by extension, the for loop) in this case, would it have to be something like this?:

for num in my_list:
    total = total + num

Indeed, that's it!