Getting output from the random Python module
Question:
I’m trying to run a script that takes five functions in a list, and picks one at random using the random
module.
myList = [quote1(), quote2(), quote3(), quote4(), quote5()]
def random_output(): random.choice(myList)
print(random_output)
However, upon running, it just prints all of the quotes at the same time, followed by <function random_output at 0x0000019F66EF4430>
.
Answers:
You have to put parentheses to invoke your function:
myList = [quote1(), quote2(), quote3(), quote4(), quote5()]
def random_output(): random.choice(myList)
print(random_output()) <--- Here
You should put your functions in myList
, not the result of their call. Then call the function after selecting it with random.choice
:
myList = [quote1, quote2, quote3, quote4, quote5]
def random_output():
# select a random function
# call it, and return its output
return random.choice(myList)()
print(random_output())
You don’t need a function A that just calls another function B. Just call function B:
import random
myList = [quote1(), quote2(), quote3(), quote4(), quote5()]
random.choice(myList)
If you really want to put it in a function, maybe because you are doing other thing in there, you should always pass in what your function needs, and return what it produces (this is called a ‘pure’ function):
def random_output(aList):
return random.choice(aList)
Then call it like:
random_output(myList)
If you don’t want to call the functions quote1
, quote2
etc yet, then you should not put parentheses after them until you do want to call them. For example:
funcs = [quote1, quote2, quote3, quote4, quote5]
random_func = random.choice(funcs)
result = random_func(my_input)
I’m trying to run a script that takes five functions in a list, and picks one at random using the random
module.
myList = [quote1(), quote2(), quote3(), quote4(), quote5()]
def random_output(): random.choice(myList)
print(random_output)
However, upon running, it just prints all of the quotes at the same time, followed by <function random_output at 0x0000019F66EF4430>
.
You have to put parentheses to invoke your function:
myList = [quote1(), quote2(), quote3(), quote4(), quote5()]
def random_output(): random.choice(myList)
print(random_output()) <--- Here
You should put your functions in myList
, not the result of their call. Then call the function after selecting it with random.choice
:
myList = [quote1, quote2, quote3, quote4, quote5]
def random_output():
# select a random function
# call it, and return its output
return random.choice(myList)()
print(random_output())
You don’t need a function A that just calls another function B. Just call function B:
import random
myList = [quote1(), quote2(), quote3(), quote4(), quote5()]
random.choice(myList)
If you really want to put it in a function, maybe because you are doing other thing in there, you should always pass in what your function needs, and return what it produces (this is called a ‘pure’ function):
def random_output(aList):
return random.choice(aList)
Then call it like:
random_output(myList)
If you don’t want to call the functions quote1
, quote2
etc yet, then you should not put parentheses after them until you do want to call them. For example:
funcs = [quote1, quote2, quote3, quote4, quote5]
random_func = random.choice(funcs)
result = random_func(my_input)