pTinosq
pTinosq

Reputation: 98

Python. Is there a way to pick a random def?

I want to store each Callout in a def function for example:

def shooting():
    print("bang bang")

def mugging():
    print("money, now!")
callout = ['shooting', 'mugging']
randomthing = random.choice(callout)
print(randomthing + "()")

So each callout is stored in the def function. Then it is randomized in callout and randomthing. Then (I know this part is wrong) I want it to call either mugging() or shooing() but with a 50/50 chance.

Upvotes: 3

Views: 476

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174624

You almost had it! Just remove the quotes, and store the function name directly. Functions in Python are just like variables:

callout = [shooting, mugging]
randomthing = random.choice(callout)
print(randomthing())

Upvotes: 7

Related Questions