How to call the random function

Question:

I have a code which has functions, which should be called randomly. However, when I am running the code below:

def print1():
    print(1)
def print2():
    print(2)
def print3():
    print(3)
l=(print1(), print2(), print3())
x=random.choice(l)
x()

It doesn’t work properly. It is outputting everything (1, 2, 3)
and raises an exception:

”NoneType’ object is not callable’

How to fix that?

Asked By: Kserdas

||

Answers:

def print1():
    print(1)
def print2():
    print(2)
def print3():
    print(3)
l=(print1, print2, print3)
x=random.choice(l)
x()

placing the functions without the brackets place the function inside the list. Writing the function with the brackets calls the function.

Btw u need not store the function into a variable, just do random.choice(l)()

Answered By: TheRavenSpectre
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.