Function Calling Python basic
<p>I'm trying to enter values from the tuple <code>('a',1), ('b',2),('c',3)</code> into the function <code>dostuff</code> but i always get a return of <code>None</code> or <code>False</code>. i'm new to this to i'm sorry if this question is basic. I would appreciate any help.</p>

<p>I expect the result of this to be:</p>

<pre><code>a1---8
b2---8 
c3---8
</code></pre>

<p>Code:</p>

<pre><code>def dostuff(stri,numb,char):
        cal = stri+str(numb)+'---'+str(char)
        return cal

def callit (tups,char):
    for x in range(len(tups)):
        dostuff(tups[x][0],tups[x][1],char)

print(callit([('a',1), ('b',2),('c',3)],8))
</code></pre>

<p>as @n1c9 said, every Python function must return some object, and if there's no <code>return</code> statement written in the function definition the function will <strong>implicitly</strong> return the <code>None</code> object. (implicitly meaning that under the hood, Python will see that there's no return statement and return<code>None</code>)</p>

<p>However, while there's nothing wrong in this case with printing a value in a function rather than returning it, it's generally considered bad practice. This is because if you ever want to test the function to aid in debugging, you have to write the test within the function definition. While if you returned the value you could just test the return value of calling the function.</p>

<p>So when you're debugging this code you might write something like this:</p>

<pre><code>def test_callit():
    tups = [('a', 1), ('b', 2), ('c', 3)]
    expected = 'a1---8\nb2---8\nc3---8'
    result = callit(tups, 8)
    assert result == expected, (str(result) + " != " + expected)
</code></pre>

<p>if you're unfamiliar with the assert statement, you can read up about it <a href="https://docs.python.org/2/reference/simple_stmts.html#grammar-token-assert_stmt" rel="nofollow">here</a></p>

<p>Now that you have a test function, you can go back and modify your code. Callit needs a return value, which in this case should probably be a string. so for the function<code>callit</code> you might write</p>

<pre><code>def callit(tups, char):
    result = ''
    for x in range(len(tups)):
        result += dostuff(tups[x][0], tups[x][1], char) + '\n'
    result = result[:result.rfind('\n')] # trim off the last \n
    return result
</code></pre>

<p>when you run test_callit, if you get any assertion errors you can see how it differs from what you expect in the traceback.</p>

<p>What I'm about to talk about isn't really relevant to your question, but I would say improves the readability of your code. </p>

<p>Python's for statement is very different from most other programming languages, because it actually acts like a foreach loop. Currently, the code ignores that feature and forces regular for-loop functionality. it's actually simpler and faster to write something like this:</p>

<pre><code>def callit(tups, char):
    result = ''
    for tup in tups:
        result += dostuff(tup[0], tup[1], char) + '\n'
    result = result[:result.rfind('\n')] # trim off the last \n
    return result
</code></pre>

python