Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting output from the random Python module

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>.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading