Trouble understanding function returns in "Functions" section of learnpython.org
<p>I'm working through the "Functions" exercise on learnpython.org and had a question about why my version of code is returning a "None" after every string that is output (where I first define the string list and then return it in that specific functions) as opposed to the solution (where they return the list of strings all in one line). Here is the code they gave me, the instructions, and what we expect to see:</p>

<p><strong>Instructions:</strong></p>

<ol>
<li><p>Add a function named list_benefits() that returns the following list of strings: "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"</p></li>
<li><p>Add a function named build_sentence(info) which receives a single argument containing a string and returns a sentence starting with the given string and ending with the string " is a benefit of functions!"</p></li>
<li><p>Run and see all the functions work together!</p></li>
</ol>

<p><strong>Code (given):</strong></p>

<pre><code># Modify this function to return a list of strings as defined above
def list_benefits():
    pass

# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
    pass

def name_the_benefits_of_functions():
    list_of_benefits = list_benefits()
    for benefit in list_of_benefits:
        print build_sentence(benefit)

name_the_benefits_of_functions()
</code></pre>

<p><strong>Expected Output:</strong></p>

<pre><code>More organized code is a benefit of functions!
More readable code is a benefit of functions!
Easier code reuse is a benefit of functions!
Allowing programmers to share and connect code together is a benefit of functions!
</code></pre>

<p>Now, this is my implementation of a solution - everything seems to be okay, except I'm getting a "None" every other line:</p>

<p><strong>Code (mine):</strong></p>

<pre><code># Modify this function to return a list of strings as defined above
def list_benefits():
    list = "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"
    return list


# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
    print "%s is a benefit of functions!" % benefit

def name_the_benefits_of_functions():
    list_of_benefits = list_benefits()
    for benefit in list_of_benefits:
        print build_sentence(benefit)

name_the_benefits_of_functions()
</code></pre>

<p><strong>Output (mine):</strong></p>

<pre><code>More organized code is a benefit of functions!
None
More readable code is a benefit of functions!
None
Easier code reuse is a benefit of functions!
None
Allowing programmers to share and connect code together is a benefit of functions!
None
</code></pre>

<p>This is what the actual solution is, which produces the Expected Output shown earlier:</p>

<p><strong>Code (solution):</strong></p>

<pre><code># Modify this function to return a list of strings as defined above
def list_benefits():
    return "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"

# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
    return "%s is a benefit of functions!" % benefit


def name_the_benefits_of_functions():
    list_of_benefits = list_benefits()
    for benefit in list_of_benefits:
        print build_sentence(benefit)

name_the_benefits_of_functions()
</code></pre>

<p>As you can see, the only difference between my code and the solution is that in mine, I'm assigning the strings to a variable "list" and then returning that variable, where as with the solution code, they are returning the list of strings themselves. What exactly is the reason why my version is returning "None" every other line, but the correct version does not?</p>

<p>Thanks!</p>

<p>The difference is in your <code>build_sentence</code> return value, not your <code>list_benefits</code> return value.</p>

<p>The line:</p>

<pre><code> print build_sentence(benefit)
</code></pre>

<p>instructs the Python interpreter to print the <em>output</em> of the <code>build_sentence</code> functionâbut if you look at your <code>build_sentence</code> implementation, you'll see that you don't explicitly <code>return</code> any output from that function. So it returns <code>None</code>, and that is what is printed.</p>

<p>Instead, your <code>build_sentence</code> takes on the job of <code>print</code>ing the result itself (which the solution code's <code>build_sentence</code> does not do). You have created an example of a function that has what's known as a "side effect"âan effect that a function has on the state of the system (or on the user) beyond just its delivery of a function output. In this case the side effect is the reason your code produces console output that looks <em>nearly</em> correct, and that misdirected you into thinking that <code>build_sentence</code> was working according to spec and focusing your attention on <code>list_benefits</code>.</p>

<p>There's no problem with <code>list_benefits</code> itself, except that it uses <code>list</code> as a variable name, overshadowing the name of one of Python's basic types. This is allowed, but it's a bad habit which sooner or later may lead to problems with code maintainability. Conventionally people often use the name <code>lst</code> when tempted to call a variable <code>list</code>.</p>

python