Issue
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?
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.