Home > Blockchain >  How to use call the random function
How to use call the random function

Time:08-22

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

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 gives an error:

''NoneType' object is not callable'

How to fix that?

CodePudding user response:

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)()

CodePudding user response:

Oh, thank you very much, because i could not do it)

  • Related