Python 3: How to run functions with parameter in random order?
<p>I knew how to run functions which doesn't have parameter. In this case, I want to run functions which has parameter in random order ? </p>

<p>It showed the result but not random and there is some error: Please help me correct the my code. Thank you :)</p>

<pre><code>import random
def hi(name):
    print('a ' + name)

def howold(old, name):
    print( name + 'is ' + old + "years old")

def howmuch(money):
    print(money + ' dollars')


functions = [hi('John'),howold('20', 'John'),howmuch('50')]
random.shuffle(functions)
for i in functions:
    i()
</code></pre>

<p>When this line executes:</p>

<pre><code>functions = [hi('John'),howold('20', 'John'),howmuch('50')]
</code></pre>

<p>Python will call your 3 functions <code>hi()</code>, <code>howold()</code> then <code>howmuch()</code> in that order, then store their result in a list called <code>functions</code>. So all <code>print()</code> will run at that point. That's why, as you said, "it showed the result but not random". Since all your functions do not return anything, your <code>functions</code> will be equal to <code>[None, None, None]</code>.</p>

<p>Then the following code:</p>

<pre><code>random.shuffle(functions)
for i in functions:
    i()
</code></pre>

<p>Will try to execute None(). That will produce an error, as you said "there is some error": this error is <code>TypeError: 'NoneType' object is not callable</code>.</p>

<p>How to fix: use for example <code>functools.partial()</code></p>

<pre><code>from functools import partial
functions = [partial(hi, 'John'), partial(howold, '20', 'John'), partial(howmuch, '50')]
random.shuffle(functions)
for i in functions:
    i()
</code></pre>

<p>Official doc here: <a href="https://docs.python.org/2/library/functools.html#functools.partial" rel="nofollow">https://docs.python.org/2/library/functools.html#functools.partial</a></p>

python function random