我试图创建一个我可以调用一个函数,它从选择的功能列表中随机函数,并调用所选择的功能转化为行动。是否可能?在这里我都试过了。这会导致什么情况发生。
import random
def ran1():
print("test1")
def ran2():
print("test2")
def ran3():
print("test3")
def ran4():
print("test4")
list_functions = [ran1,ran2,ran3,ran4]
def ran_choice():
random.choice(list_functions)
ran_choice()
你的逻辑是罚款。你只需要打电话给你的ran_choice
选择功能:
def ran_choice():
random.choice(list_functions)()
虽然,它可能更容易为已读:
def ran_choice():
chosen_fn = random.choice(list_functions)
chosen_fn()