选择随机函数

时间:2019-10-23 15:08:39

标签: python-3.x

我不想在python程序中选择随机函数。 我有以下代码: 我收到错误消息:NameError:未定义名称“好”

我在做什么错,请帮忙。

    import random

class functions:
    def jammer():
        print("jammer")

    def goedzo():
        print("goedzo")

    def uuh():
        print("uuh")

    def tsjing():
      print("tsjing")

 #random functonlist
    ls_good = [goedzo, tsjing]
    ls_wrong = [jammer, uuh]

    #method to select different functions
    #def __init__(self, g = random.choice(ls_good), f = random.choice(ls_wrong)):
    def randomfunction(self, good = random.choice(ls_good), wrong = random.choice(ls_wrong)):
        self.good = good
        self.wrong = wrong
        return self.good
        return self.wrong

#create object from class playsound
selected_function = functions()
selected_function.randomfunction(good, wrong)

2 个答案:

答案 0 :(得分:2)

主模块中没有名为111good的变量。但是,您将它们传递给了此功能:

wrong

答案 1 :(得分:0)

thnx为您解答! 通过这样做,我设法得到了我不想要的东西:

    import random

class functions:
    def jammer():
        print("jammer")

    def goedzo():
        print("goedzo")

    def uuh():
        print("uuh")

    def tsjing():
      print("tsjing")

 #random functonlist
    ls_good = [goedzo, tsjing]
    ls_wrong = [jammer, uuh]

    #method to select different functions
    def randomfunction(self, test, good = random.choice(ls_good), wrong = random.choice(ls_wrong)):
        self.good = good
        self.wrong = wrong
        self.test = test
        if self.test == 1:
            good()
        else:
            wrong()
#create object from class playsound
sf = functions()
sf.randomfunction(test=1)