-3

I want to use recursion to calculate the sum of the list values, but there is an error when using the sum2 function: TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

def sum(list):
    if list == []: 
        return 0 
    else:
        return list[0] + sum(list[1:])
print(sum([1,2,3]))

def sum2(list):
    if list == []: 
        return 0 
    else:
        print(list[0] + sum(list[1:]))
sum([1,2,3])
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
flystar
  • 85
  • 1
  • 1
  • 8
  • `sum2` does not return anything – Stephen Rauch Mar 20 '18 at 03:07
  • In python 3 [print](https://docs.python.org/3/library/functions.html#print) is a function that prints to the console. [return](https://docs.python.org/3/reference/simple_stmts.html#the-return-statement) is a type of statement that ends execution of a function and _returns_ the specified value to whoever called that function. But your question isn't really about that- its about how to use recursion – avigil Mar 20 '18 at 03:11
  • *What's the difference between print and return?* is like asking *What's the difference between an orange and an airplane?* They're not the same thing at all. Think about the meaning of the words themselves; you *return a borrowed book* and *print a letter*. Find a Python tutorial; there's a list of many of them available at [Python.org](http://python.org) – Ken White Mar 20 '18 at 03:15
  • @KenWhite I feel your pain, but this is a pretty common beginner's question, especially given the way tutorials tend to start: `print()`ing things for the beginner to see in the console. It's easy enough to answer directly, without the exasperated tone :) – De Novo Mar 20 '18 at 03:23
  • 1
    Does this answer your question? [How is returning the output of a function different from printing it?](https://stackoverflow.com/questions/750136/how-is-returning-the-output-of-a-function-different-from-printing-it) – John Kugelman Oct 06 '20 at 16:34

2 Answers2

1

print() sends output to your console via standard output. return sends output to whatever called your function. If you want to use recursion, you need to use a return statement, not print().

Here's an example:

def sum2(l):
    if l == []:
        return 0
    else:
        return l[0] + sum2(l[1:])

sum2([1, 2, 3])
# 6

This is recursive because the return statement contains a call to the function itself. Generally, a good thing to learn about in a computer science class but a bad thing to do in production code.

De Novo
  • 7,120
  • 1
  • 23
  • 39
0

I didn't understand what's your requirement by looking in to your code, but as I understood you need to know the difference between print and return .

print is a function which requires a print description and printing variables or object. This function will helps the developer to debug and check the console output. Where as return is a keyword and this will be used in any function or method to return some value from that function or method to it's caller .

Jayachandra A
  • 1,335
  • 1
  • 10
  • 21