蟒蛇。有没有办法选择一个随机的防御?

时间:2018-06-25 20:35:59

标签: python function random

我想将每个标注存储在def函数中 例如:

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

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

因此每个标注都存储在def函数中。然后在标注和随机数中将其随机化。然后(我知道这部分是错误的),我希望它调用mugging()或shooing(),但是机会是50/50。

1 个答案:

答案 0 :(得分:6)

您几乎拥有了!只需删除引号,然后直接存储函数名称。 Python中的函数就像变量一样:

callout = [shooting, mugging]
randomthing = random.choice(callout)
print(randomthing())
相关问题